我在python中使用此代码向udp跟踪器发送连接请求和annouce请求,
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print clisocket
connection_id = 0x41727101980
transaction_id = randrange(1, 65535)
info_hash = info_hash
peer_id = "ABCDEFGHIJKLMNOPQRST"
action = 0
downloaded = 0
left = 0
uploaded = 0
event = 2
ip = 0
key = 0
num_want = 10
port = 9999
connect_pack = struct.pack(">QLL", connection_id, action, transaction_id)
clisocket.sendto(connect_pack, ("tracker.publicbt.com", 80))
res = clisocket.recv(16)
action, transaction_id, connection_id = struct.unpack(">LLQ", res)
announce_pack = struct.pack(">QLL20s20sQQQLLLLH", connection_id, 1, transaction_id, info_hash, peer_id, downloaded, left, uploaded, event, ip, key, num_want, port)
clisocket.sendto(announce_pack, ("tracker.publicbt.com", 80))
res = clisocket.recv(1024)
action = struct.unpack("!LLLLLLH", res[:26])
print action
我收到了这个回复,
(1, 56347, 1782, 0, 1, 838084381, 9999)
根据协议规范[link]:http://www.bittorrent.org/beps/bep_0015.html
我到了 annouce即1 transaction_id,即56347,与生成的transaction_id相同 但是代替端口我正在收到我在发布请求中发送的端口,而不是IP,我得到了838084381。 这是我应该从udp跟踪器获得的响应吗? 如何解析同行的ip?
答案 0 :(得分:1)
IP地址以32位打包格式返回。您可以将其转换为点状四字符串表示形式,如下所示:
import socket, struct
ip = socket.inet_ntoa(struct.pack('!L', 838084381))
print ip; # prints 49.244.39.29