torrent客户端:向对等体发送握手消息

hel*_*lp 0 python bittorrent handshake

从我得到对等列表,我得到一个tcp连接到同行,我试图给他们一个握手消息,但他们似乎没有回应.

这是我在代码中的消息:

message = bytes(chr(19))+"BitTorrent protocol00000000"+self.getInfoHash(torrentCont)+self.peer_id
Run Code Online (Sandbox Code Playgroud)

self.getInfoHash(torrentCont)是torrent文件中的原始哈希

这是我发送的实际内容:

BitTorrent protocol00000000ŒïƒœÝtDØ´öÙÄ×àŠD³T4F11T6ZGBQK2Y5LB8I4
Run Code Online (Sandbox Code Playgroud)

关于我做错了什么的任何建议?

Arm*_*igo 5

你是混乱的字节和字符.规范说的是你应该发送8个空字节,而不是字符"0"的八倍(这是chr(48)):

message = (chr(19) +
           "BitTorrent protocol" +
           8 * chr(0) +               # <--- here
           self.getInfoHash(torrentCont) +
           self.peer_id)

# in case of doubt...
assert len(self.getInfoHash(torrentCont)) == 20
assert len(self.peer_id) == 20
Run Code Online (Sandbox Code Playgroud)