php - 如何逐字节发送/接收数据

标签 php python client-server

我正在从客户端上的 python 脚本触发我的网络服务器上的 php 脚本。我正在处理可供客户端脚本解析的二进制数据,如下所示:

$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;

现在这可以很好地处理这些数据,但是如何将我的文件 $file 附加到该流的末尾以供我的客户拾取? 在Python方面,我从fh = StringIO(data)读取所有数据,其中我得到像MyByte = ord(fh.read(1))这样的字节p>

[编辑] 我只是尝试在末尾(在回显之前)附加数据,例如:

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

但这似乎不起作用,为什么不......?

最佳答案

由于您正在输出数据流,因此我认为您没有任何理由不能直接回显整个文件内容。我还将 $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);

关于php - 如何逐字节发送/接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13483287/

相关文章:

php - 奇怪的主机问题

php - Magento更新每商店价格查看SQL

python - 如何在Python中将JSON字符串保存为CSV或TXT?

php - ELO评级-mysql设计

python - 带小数秒的时间字符串

python - Pandas 会影响 Rapidfuzz 匹配的结果吗?

c - 桌面应用程序需要服务器端调试

php - 从客户端向服务器发送 30 个变量的最佳方式是什么?

java - 从 .class 文件创建实例时出现问题

php - php中的require_once没有这样的文件或目录错误