Not receiving any data back from bittorrent peer h

2019-07-14 20:33发布

问题:

I'm having some trouble on the bit torrent protocol. I'm at the point of sending a handshake message to some peers. I have my client basically connect to every peer in list then send the 'handshake'. Code is below -

peer_id = 'autobahn012345678bit'
peer_id = peer_id.encode('utf-8')
pstr = 'BitTorrent protocol'
pstr = pstr.encode('utf-8')
pstrlen = chr(19)
pstrlen = pstrlen.encode('utf-8')
reserved = chr(0) * 8
reserved = reserved.encode('utf-8')

There are my variables that I'm sending. My msg is -

msg = (pstrlen + pstr + reserved + new.torrent_hash() + peer_id)

Based on the bit torrent specification my message is the appropriate len of 49 + len(pstr) -

lenmsg = (pstrlen + reserved + new.torrent_hash() + peer_id)

print(lenmsg)
print(len(lenmsg))

is out put -

b'\x13\x00\x00\x00\x00\x00\x00\x00\x00\x94z\xb0\x12\xbd\x1b\xf1\x1fO\x1d)\xf8\xfa\x1e\xabs\xa8_\xe7\x93autobahn012345678bit'
49

the entire message looks like this -

b'\x13\x00\x00\x00\x00\x00\x00\x00\x00\x94z\xb0\x12\xbd\x1b\xf1\x1fO\x1d)\xf8\xfa\x1e\xabs\xa8_\xe7\x93autobahn012345678bit'

My main problem being I don't receive any data back. I have the socket.settimeout(4) and it'll just timeout?

回答1:

The output is incorrect, it misses 'BitTorrent protocol'.
A proper handshake string is 68 bytes long.

It should be:

\x13BitTorrent protocol\x00\x00\x00\x00\x00\x00\x00\x00\x94z\xb0\x12\xbd\x1b\xf1\x1fO\x1d)\xf8\xfa\x1e\xabs\xa8_\xe7\x93autobahn012345678bit