Search code examples
python-3.xbittorrentpython-3.5handshake

Why doesn't peer send handshake message in response to the handshake message I send?


I've started writing my own BitTorrent client in Python 3 recently. And all was perfect until I faced the following issue: Instead of a response handshake when I send a formatted handshake message to one of the peers, I do not get anything (b'' when buff is not decoded). Here is the code:

handshakemsg = chr(19)+"BitTorrent protocol"+8*chr(0)+
        getinfohash()+"HAHA-0142421214125A-")
s.send(handshakemsg.encode())
print("Connection to peer accepted")
buff = s.recv(len(handshakemsg))
print(buff)

That's the proper way of sending handshake message I think, but the response doesn't look like the one described in the specification. I'm wondering why is that happening, and how can I avoid that?


Solution

  • http://bittorrent.org/beps/bep_0003.html#peer-protocol

    After the fixed headers come eight reserved bytes, which are all zero in all current implementations. If you wish to extend the protocol using these bytes, please coordinate with Bram Cohen to make sure all extensions are done compatibly. Next comes the 20 byte sha1 hash of the bencoded form of the info value from the metainfo file.

    Yours is 40 (hex-encoded). Bittorrent is a binary protocol, not text.