Skip to main content

Python Distributed Hash Table

Build Status Docs Status

Documentation can be found at kademlia.readthedocs.org.

This library is an asynchronous Python implementation of the Kademlia distributed hash table. It uses the asyncio library in Python 3 to provide asynchronous communication. The nodes communicate using RPC over UDP to communiate, meaning that it is capable of working behind a NAT.

This library aims to be as close to a reference implementation of the Kademlia paper as possible.

Installation

pip install kademlia

Usage

This assumes you have a working familiarity with asyncio.

Assuming you want to connect to an existing network:

import asyncio
from kademlia.network import Server

async def run():
    # Create a node and start listening on port 5678
    node = Server()
    await node.listen(5678)

    # Bootstrap the node by connecting to other known nodes, in this case
    # replace 123.123.123.123 with the IP of another node and optionally
    # give as many ip/port combos as you can for other nodes.
    await node.bootstrap([("123.123.123.123", 5678)])

    # set a value for the key "my-key" on the network
    await node.set("my-key", "my awesome value")

    # get the value associated with "my-key" from the network
    result = await node.get("my-key")
    print(result)

asyncio.run(run())

Initializing a Network

If you're starting a new network from scratch, just omit the node.bootstrap call in the example above. Then, bootstrap other nodes by connecting to the first node you started.

See the examples folder for a first node example that other nodes can bootstrap connect to and some code that gets and sets a key/value.

Logging

This library uses the standard Python logging library. To see debut output printed to STDOUT, for instance, use:

import logging

log = logging.getLogger('kademlia')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())

Running Tests

To run tests:

uv sync --dev
uv run pytest

Reporting Issues

Please report all issues on github.

Fidelity to Original Paper

The current implementation should be an accurate implementation of all aspects of the paper save one - in Section 2.3 there is the requirement that the original publisher of a key/value republish it every 24 hours. This library does not do this (though you can easily do this manually).

Metadata

Release files for kademlia 2.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kademlia 2.2.3
File Size Uploaded
kademlia-2.2.3.tar.gz 55.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kademlia 2.2.3
File Interpreter ABI Platform
kademlia-2.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 74.5 kB

Release files / kademlia-2.2.3.tar.gz

Download URL kademlia-2.2.3.tar.gz
Size 55.3 kB
Tags Source
SHA-256 checksum
How to use checksums
33f4d89e6429648cc7554684420a5a759b2b631b0ac72a80c7844732be48d48e
BLAKE2b-256 checksum
How to use checksums
34d123677598858c2c00bd638a666a677af3d49cb122fc130d31a08600676156
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release files / kademlia-2.2.3-py3-none-any.whl

Download URL kademlia-2.2.3-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7dac4b277ddef2b1e6cb0f74a70c970d3cd6790b5163b240e38bf48ed22a248d
BLAKE2b-256 checksum
How to use checksums
5c94db40b80444f9e4c0816735d781e191d96a48f26dc33131d5bd6bf0accc30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release history Release notifications | RSS feed

This release

2.2.3 This release

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2

2 release files

2.1

2 release files

2.0

2 release files

1.1

2 release files

1.0

2 release files

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page