现有 DHT 的 Hello World

发布于 2024-11-09 07:53:54 字数 124 浏览 0 评论 0原文

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

I am familiar with the theory of how a Distributed Hash Table (DHT) works. Is it possible to write a program that stores data to an existing DHT (such as Kademlia or Mainline DHT) ? Is there a simple 'Hello World' type of program that would show the simplest possible way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀 2024-11-16 07:53:54

DHT 的最佳 hello world 是将 Bittorrent 的 DHT 上的 'ping' 发送到引导节点。步骤是:

  1. Bencode a KRPC PING 消息。
  2. 通过 UDP引导节点
  3. 等待回复。

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

The best hello world for DHT would be to send a 'ping' on Bittorrent's DHT to a bootstrap node. The steps are:

  1. Bencode a KRPC PING message.
  2. Send it over UDP to a bootstrap node.
  3. Wait for a reply.

These are the steps I just took before I began working on my own DHT implementation.

策马西风 2024-11-16 07:53:54

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

如前所述,向现有 DHT 说“Hello”的最简单方法是向 DHT 节点之一发送 ping 消息。让我们考虑基于 Kademlia 的主线 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 对消息进行 bencode 和 bdecode 的模块。

有关 Mainline DHT 协议的更多信息,请参阅本文档。 (请注意,该协议与原始 Kademlia 协议略有不同。)

The question is might be outdated but anyway.

As was mentioned, the simplest way to say "Hello" to an existing DHT is to send a ping message to one of DHT nodes. Let's consider Kademlia-based Mainline DHT (MDHT).

There is a bootstrap server at address router.bittorrent.com on port 6881. You can think about this server as a general DHT node which is permanently online. Also, you can use another node such as locally run torrent client, which uses DHT.

I've written a small example in 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)

I've used bencode module to bencode and bdecode messages.

More information on Mainline DHT protocol can be in this document. (Note that the protocol is slightly different from original Kademlia protocol.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文