问题描述
我在 bit Torrent 协议上遇到了一些问题.我正在向一些对等方发送握手消息.我的客户端基本上连接到列表中的每个对等点,然后发送握手".代码如下 -
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)
根据比特洪流规范,我的消息是 49 + len(pstr) 的适当长度 -
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))
输出 -
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'
我的主要问题是我没有收到任何数据.我有 socket.settimeout(4)
并且它会超时?
My main problem being I don't receive any data back. I have the socket.settimeout(4)
and it'll just timeout?
推荐答案
输出不正确,缺少BitTorrent 协议".
正确的握手字符串长度为 68 个字节.
The output is incorrect, it misses 'BitTorrent protocol'.
A proper handshake string is 68 bytes long.
应该是:
\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
这篇关于没有从 bittorrent 对等握手中收到任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!