问题
I am sending a request to a tracker and get a response
d8:completei2e10:downloadedi1e10:incompletei1e8:intervali1971e12:min intervali985e5:peers18:\235'\027\253\000\000\331e57\374-\033"\022,\270\302e
How to get Peers list or peer IP and port from this response
回答1:
The response from the tracker is bencoded.
Adding some whitespace for clarity:
d
8:complete
i2e
10:downloaded
i1e
10:incomplete
i1e
8:interval
i1971e
12:min interval
i985e
5:peers
18:\235'\027\253\000\000\331e57\374-\033"\022,\270\302
e
The key:peers that has a 18 bytes binary string as value contains peers in the 'compact=1'-form that is specified in: [BEP23 - Tracker Returns Compact Peer Lists] and also in [the wiki]
Every peer is represented by 6 bytes, 4 bytes IPv4 + 2 bytes PORT in bigendian, so the 18 bytes string is for 3 peers.
\235 ' \027 \253 \000 \000=>157 39 23 171 0 0(0*256+0=0) =>157.39.23.171:0
\331 e 5 7 \374 - =>227 101 53 55 252 45(252*256+45=64557)=>227.101.53.55:64557
\033 " \022 , \270 \302=>27 34 18 44 184 192(184*256+192=47298)=>27.34.18.44:47298
(\235 is octal for 157, '
has ASCII value 39 etc.)
来源:https://stackoverflow.com/questions/50094674/how-to-parse-ip-and-port-from-http-tracker-response