Python Distributed Hash Table
![Coverage Status](https://coveralls.io/repos/github/bmuller/twistar/badge.svg?branch=master)
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():
node = Server()
await node.listen(5678)
await node.bootstrap([("123.123.123.123", 5678)])
await node.set("my-key", "my awesome value")
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).