python - 没有从 bittorrent 对等握手中接收到任何数据

标签 python tcp bittorrent asyncore

我在 bit torrent 协议(protocol)上遇到了一些问题。我正在向一些同行发送握手消息。我让我的客户基本上连接到列表中的每个对等点,然后发送“握手”。代码如下 -

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')

我正在发送我的变量。我的消息是 -

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

根据比特流规范,我的消息是 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

整个消息看起来像这样 -

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) 它会超时吗?

最佳答案

输出不正确,它遗漏了“BitTorrent 协议(protocol)”。
正确的握手字符串长度为 68 字节。

应该是:

\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

关于python - 没有从 bittorrent 对等握手中接收到任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985955/

相关文章:

python - 什么是周期?关于 python

C# 异步对象传输(套接字?)

java - 如何使用UDP或TCP发送压缩数据(lua/java)

python - 查找给定 torrent 文件的播种者/对等者 IP 地址的最佳方法是什么

python http/udp bittorrent 跟踪器抓取库

network-protocols - BitTorrent 协议(protocol)似乎与现实不符

python - 使用 sleep() 时 Python 运动中的代码不流畅

python - 如何从类中的方法获取一个值而不重新计算?

Python 2.x 与 3.x 导入,具体取决于调用/导入脚本的位置

c - 如何使用 TCP/IP 协议(protocol)从 Android 接收文本文件到 C。我有数据丢失