使用python中的struct模块在pack()中选择格式

时间:2011-10-13 18:05:51

标签: php python sockets network-programming

我正在尝试将PHP代码转换为python。

所有值都以网络字节顺序(大端)发送。

基本上,协议规范中的REQUEST是

enter image description here

并且回复是

enter image description here

对应的PHP代码(corresponding DOC)是:

$transaction_id = mt_rand(0,65535);
$current_connid = "\x00\x00\x04\x17\x27\x10\x19\x80";
$fp = fsockopen($tracker, $port, $errno, $errstr);
$packet = $current_connid . pack("N", 0) . pack("N", $transaction_id);
fwrite($fp,$packet);

我试图在python中找到相应的代码(for doc):

transaction_id = random.randrange(1,65535)
packet = "\x00\x00\x04\x17\x27\x10\x19\x80"
packet = packet + struct.pack("i", 0) + struct.pack("i", transaction_id)
clisocket =  socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
clisocket.sendto(packet, ("tracker.istole.it", 80))

在响应中,我应该得到我在请求中发送的相同transaction_id,而我没有得到。所以,我的猜测是,我没有使用正确的格式打包。

此外,python文档并不像PHP那样清晰。该协议规定使用Big Endian格式& PHP文档清楚地说明哪些是Big-Endian的。

可悲的是,我无法理解在python中使用哪种格式。请帮我选择正确的格式。

修改 没有得到任何回复,所以我会说更多。

import struct
import socket
import random

clisocket =  socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
packet = "\x00\x00\x04\x17\x27\x10\x19\x80"
transaction_id = random.randrange(1,65535)
print transaction_id
packet = packet+struct.pack(">i", 0)
packet = packet+struct.pack(">i", transaction_id)

clisocket.sendto(packet, ("tracker.istole.it", 80))
res = clisocket.recv(16)

print struct.unpack(">i", res[12:16])

根据协议规范,我应该返回相同的INTEGER。

2 个答案:

答案 0 :(得分:3)

php pack function格式N表示无符号32位大端整数。 相应的Python struct.pack格式为>L

您为协议发布的图像显示connection_id应为64位(无符号)整数:Python struct.pack format Q

所以:

clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
connection_id=0x41727101980
action=0
# transaction_id = random.randrange(1,65535)
transaction_id = 12345    
print(transaction_id)
# 12345

packet=struct.pack(">QLL",connection_id,action,transaction_id)
print(repr(packet))
# "\x00\x00\x04\x17'\x10\x19\x80\x00\x00\x00\x00\x00\x0009"

clisocket.sendto(packet, ("tracker.istole.it", 80))
res = clisocket.recv(16)
action,transaction_id,connection_id=struct.unpack(">LLQ",res)
print(action)
# 0
print(transaction_id)
# 12345 
print(connection_id)
# 2540598739861590271

答案 1 :(得分:0)

§7.3.2.1 of the library reference中描述了字节顺序。 Big-endian打包的前缀为>