A DHT in Python using asyncio
Go to file
2021-04-12 10:07:48 -04:00
.github Create FUNDING.yml 2019-09-05 08:26:29 -04:00
docs Updated requirements, examples, README, and CHANGELOG 2020-05-02 21:36:33 -04:00
examples let example node.py run as second node server 2021-04-12 10:07:48 -04:00
kademlia let example node.py run as second node server 2021-04-12 10:07:48 -04:00
.gitignore Add MANIFEST.in, release version 2.2.2 2021-02-04 12:35:57 -05:00
.pylintrc removed all camelcase silliness, bumped version to 2.0 2019-01-09 11:27:10 -05:00
.travis.yml update tests without unittest only pytest (#62) 2019-06-15 10:44:53 -04:00
CHANGELOG.md Add MANIFEST.in, release version 2.2.2 2021-02-04 12:35:57 -05:00
dev-requirements.txt Updated requirements, examples, README, and CHANGELOG 2020-05-02 21:36:33 -04:00
LICENSE upgraded testing methods, dev dependencies, and travis config 2018-01-01 17:07:40 -05:00
MANIFEST.in MANIFEST.in: Include LICENSE and *.md (#93) 2021-02-05 19:28:24 -05:00
pytest.ini update tests without unittest only pytest (#62) 2019-06-15 10:44:53 -04:00
README.md Updated requirements, examples, README, and CHANGELOG 2020-05-02 21:36:33 -04:00
requirements.txt Updated requirements, examples, README, and CHANGELOG 2020-05-02 21:36:33 -04:00
setup.py minor version update to handle long_description_content_type for pypi 2019-02-04 09:00:47 -05:00

Python Distributed Hash Table

Build Status Docs Status Coverage 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:

pip install -r dev-requirements.txt
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).