我正在尝试使用 python 实现 DHT 协议,但无法从公共 DHT 节点 (router.bittorrent.com:6881) 获取响应。有趣的是,我编写的 Node JS 变体运行良好,并且我能够从服务器获得响应。我使用的是 Windows,并在打开所有 UDP 端口的防火墙设置的入站连接中添加了 UDP 端口规则。我还尝试将整个防火墙全部禁用,但无济于事。我将不胜感激任何意见。
#info_hash is a byte string parameter
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(3)
network_info = (socket.gethostbyname('router.bittorrent.com'), 6881)
sock.connect(network_info)
rand = lambda: ''.join([chr(random.randint(0, 255)) for _ in range(20)])
my_id = rand()
get_peers = {b"t":b'aa', b"y":b"q", b"q":b"get_peers",
b"a": {b"id":my_id,
b"info_hash": info_hash}}
encoded_data = bencoding.encode(get_peers)
sock.send(encoded_data)
res = sock.recv(1024) #Always times out here
我添加了防火墙规则(并禁用了Windows设置中的所有防火墙保护措施),尝试将套接字绑定到(0.0.0.0),并尝试使用linux(kali linux虚拟机)
find_node
,而不是 get_peers