简体   繁体   中英

How to parse the IP and port of the peers from UDP tracker annouce response

I am using this code in python to send connect request and annouce request to udp tracker,

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

I am getting this response,

(1, 56347, 1782, 0, 1, 838084381, 9999)

as per the protocol specifications [link] : http://www.bittorrent.org/beps/bep_0015.html

I am getting annouce ie 1
transaction_id ie 56347 which is same as the generated transaction_id but in place of port I am getting the port I am sending in the announce request and in place of IP I am getting 838084381. Is this the response that I should get from the udp tracker? How to parse the ip of the peers from this?

The IP address is returned in a 32-bit packed format. You can convert it to a dotted-quad string representation like this:

import socket, struct    
ip = socket.inet_ntoa(struct.pack('!L', 838084381))    
print ip; # prints 49.244.39.29

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM