2012-11-20 35 views
1

我從我的客戶端上的python腳本觸發我的web服務器上的php腳本。我處理的二進制數據,我提供給我的客戶端腳本解析這樣的:如何發送/接收數據Byte Bytete

$file = "gw/gateway.py" 
if (file_exists($file)) { 
    $gw_file_sz = filesize($file); 
    $filesz1 = $gw_file_sz/256; 
    $filesz2 = $gw_file_sz%256; 
} 
    $binarydata = pack("C*", 0x01, $year1, $year2, $day1, $day2, $min1, $min2, $sec, 0x00, 0x3f, 0x02, 0x00, 0x1c, 0x2c , 0x4c, 0xdf, 0xcb, 
                          0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe8, 0x41, 
               0x04, 0x00, 0x1c, 0x2c , 0x5c, 0xe4, 0x38, 
               0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe3, 0x7b, 
               0x02, 0x00, 0x1c, 0x2c , 0x4c, 0xdf, 0xbf, 
               0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe7, 0xd7, 
               0x02, 0x00, 0x1c, 0x2c , 0x4c, 0xdf, 0x64, 
               0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe7, 0x7a, 
               0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe8, 0x22, 
               0x08, $filesz1, $filesz2); 

echo $binarydata; 

現在,這正常工作與這些數據,但我怎麼追加我的文件$文件在此流的末尾是由我的客戶拿起? 在Python端,我讀出來自FH = StringIO的(數據)中的所有數據哪裏獲得字節像MyByte = ORD(fh.read(1))

[編輯] 我只是試圖在末尾附加數據(回聲之前),如:

$fh = fopen($file); 
    for ($i=0;$i<filesize($file); $i++) { 
     $binarydata.=pack("C*",fread($fh,1)); 
    } 
    fclose($fh); 

但它似乎沒有工作,爲什麼不... ...?

回答

1

由於您正在輸出數據流,因此我沒有看到任何原因,您不能直接回顯整個文件內容。我也修改$ filesz1到我認爲你可能打算的目標。

$file = "gw/gateway.py" 
if (file_exists($file)) { 
    $gw_file_sz = filesize($file); 
    $filesz1 = floor($gw_file_sz/256); 
    $filesz2 = $gw_file_sz%256; 
} 
$binarydata = pack("C*", 0x01, $year1, $year2, $day1, $day2, $min1, $min2, $sec, 
    0x00, 0x3f, 0x02, 0x00, 0x1c, 0x2c , 0x4c, 0xdf, 0xcb, 
    0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe8, 0x41, 
    0x04, 0x00, 0x1c, 0x2c , 0x5c, 0xe4, 0x38, 
    0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe3, 0x7b, 
    0x02, 0x00, 0x1c, 0x2c , 0x4c, 0xdf, 0xbf, 
    0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe7, 0xd7, 
    0x02, 0x00, 0x1c, 0x2c , 0x4c, 0xdf, 0x64, 
    0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe7, 0x7a, 
    0x02, 0x00, 0x1c, 0x2c , 0x5c, 0xe8, 0x22, 
    0x08, $filesz1, $filesz2); 

echo $binarydata; 
echo file_get_contents($file); 
+0

嗨JMack,感謝您的迴應,這樣一切都會在同一個流,不會打開一個新的file_get_contents()? – cerr

+0

不,它不應該打開一個新的流。它應該都是一個流。這與'echo $ binarydata.file_get_contents($ file);'相同,它會在回顯所有數據之前從所有數據創建一個字符串。 –