我似乎无法从Python脚本中获得与PHP脚本(由网站主机编写,运行良好)相同的数据。
PHP连接到与Python脚本相同的位置。
在任何人急于下结论之前,我知道Python脚本仅检索部分数据。 但是我无法从服务器获取大致相同的数据。
Python:
import socket, struct
host,port = 'baystation12.net', 8000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send('status\r\n')
data = s.recv(1024)
s.close()
print 'Received:', repr(data) # >>> Received: '\x00\xc7\x00\x07\x02\xadj\x00\x00\x1c\xf6'
cache,form,listy = "",">H",[]
for i in data:
if cache != "":
listy.append(struct.unpack(form,cache+i))
else:
cache = i
print "Unpacked:",listy # >>> Unpacked: [(199,), (0,), (7,), (2,), (173,), (106,), (0,), (0,), (28,), (246,)]
text = ""
for i in listy:
text += chr(i[0])
print "Text:",text # >>> Text: Ç
#Shows up incorrectly when I try to copy it.
PHP:
#!/usr/bin/php
<?php
function export($addr,$port,$str)
{
if($str{0} != "?") $str = ("?" . $str);
$query = "\x00\x83" . pack("n",strlen($str)+6) . "\x00\x00\x00\x00\x00" . $str . "\x00";
$server = socket_create(AF_INET,SOCK_STREAM,SOL_TCP) or exit('Unable to create export socket; ' . socket_strerror(socket_last_error()));
socket_connect($server,$addr,$port) or exit('Unable to establish socket connection; ' . socket_strerror(socket_last_error()));
$bytessent = 0;
while($bytessent < strlen($query))
{
$result = socket_write($server,substr($query,$bytessent),strlen($query)-$bytessent);
if($result === FALSE) return('Unable to transfer requested data; ' . socket_strerror(socket_last_error()));
$bytessent += $result;
}
$resbuf = '';
while( socket_recv($server, $message,1,0 )){
$resbuf .= $message;
if(strpos($resbuf,"&end")!=FALSE)
{
echo $resbuf;
socket_close($server);
return($resbuf);
}
echo $message;
};
echo $resbuf."\n";
socket_close($server);
}
export("localhost","8000","status");
?>
PHP的输出:
version=Baystation+12&mode=extended&respawn=0&enter=1&vote=1&ai=1&host&players=5&player0=CompactNinja&player1=Sick+trigger&player2=SweetJealousy&player3=Cacophony&player4=Anchorshag&end
有没有想过为什么Python在解压数据时会输出无意义的字符,而PHP则输出以上内容?