【问题标题】:Hello World for an existing DHT现有 DHT 的 Hello World
【发布时间】:2011-05-25 10:49:45
【问题描述】:

我熟悉分布式哈希表 (DHT) 的工作原理。是否可以编写将数据存储到现有 DHT(例如 Kademlia 或 Mainline DHT)的程序?是否有一个简单的“Hello World”类型的程序可以显示最简单的方法?

【问题讨论】:

    标签: p2p bittorrent dht


    【解决方案1】:

    对于 DHT 来说,最好的 hello world 是将 Bittorrent 的 DHT 上的 'ping' 发送到引导节点。步骤是:

    1. Bencode KRPC PING 消息。
    2. SendUDP 转换为 bootstrap node
    3. Wait 回复。

    这些是我在开始实施自己的 DHT 之前刚刚采取的步骤。

    【讨论】:

      【解决方案2】:

      这个问题可能已经过时了,但无论如何。

      如前所述,向现有 DHT 说“Hello”的最简单方法是向其中一个 DHT 节点发送ping 消息。让我们考虑基于 Kademlia 的 Mainline DHT (MDHT)。

      在端口6881 上的地址router.bittorrent.com 有一个引导服务器。您可以将此服务器视为永久在线的通用 DHT 节点。此外,您可以使用其他节点,例如本地运行的 Torrent 客户端,它使用 DHT。

      我用 Python 写了一个小例子:

      import bencode
      import random
      import socket
      
      
      # Generate a 160-bit (20-byte) random node ID.
      my_id = ''.join([chr(random.randint(0, 255)) for _ in range(20)])
      
      # Create ping query and bencode it.
      # "'y': 'q'" is for "query".
      # "'t': '0f'" is a transaction ID which will be echoed in the response.
      # "'q': 'ping'" is a query type.
      # "'a': {'id': my_id}" is arguments. In this case there is only one argument -
      # our node ID.
      ping_query = {'y': 'q',
                    't': '0f',
                    'q': 'ping',
                    'a': {'id': my_id}}
      ping_query_bencoded = bencode.bencode(ping_query)
      
      # Send a datagram to a server and recieve a response.
      s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      s.sendto(ping_query_bencoded,
               (socket.gethostbyname('router.bittorrent.com'), 6881))
      r = s.recvfrom(1024)
      
      ping_response = bencode.bdecode(r[0])
      
      print(ping_response)
      

      我使用bencode 模块对消息进行了编码和解码。

      更多关于 Mainline DHT 协议的信息可以在this document。 (请注意,该协议与原始 Kademlia 协议略有不同。)

      【讨论】:

        猜你喜欢
        • 2019-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 2014-10-15
        • 2023-04-11
        • 2011-09-12
        相关资源
        最近更新 更多