kademlia/README.md

74 lines
3.0 KiB
Markdown
Raw Normal View History

# Python Distributed Hash Table
2014-01-04 20:27:45 +01:00
[![Build Status](https://secure.travis-ci.org/bmuller/kademlia.png?branch=master)](https://travis-ci.org/bmuller/kademlia)
2015-01-05 19:51:33 +01:00
[![Docs Status](https://readthedocs.org/projects/kademlia/badge/?version=latest)](http://kademlia.readthedocs.org)
**Documentation can be found at [kademlia.readthedocs.org](http://kademlia.readthedocs.org/).**
2014-01-03 03:39:06 +01:00
This library is an asynchronous Python implementation of the [Kademlia distributed hash table](http://en.wikipedia.org/wiki/Kademlia). It uses the [asyncio library](https://docs.python.org/3/library/asyncio.html) in Python 3 to provide asynchronous communication. The nodes communicate using [RPC over UDP](https://github.com/bmuller/rpcudp) to communiate, meaning that it is capable of working behind a [NAT](http://en.wikipedia.org/wiki/Network_address_translation).
This library aims to be as close to a reference implementation of the [Kademlia paper](http://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf) as possible.
2014-01-03 03:39:06 +01:00
## Installation
```
2014-01-04 20:27:45 +01:00
pip install kademlia
2014-01-03 03:39:06 +01:00
```
## Usage
2018-01-02 19:14:25 +01:00
*This assumes you have a working familiarity with [asyncio](https://docs.python.org/3/library/asyncio.html).*
2014-01-03 03:39:06 +01:00
2018-01-02 19:14:25 +01:00
Assuming you want to connect to an existing network:
2014-01-03 03:39:06 +01:00
```python
2018-01-02 19:14:25 +01:00
import asyncio
2014-01-04 20:27:45 +01:00
from kademlia.network import Server
loop = asyncio.get_event_loop()
2018-01-02 19:14:25 +01:00
# Create a node and start listening on port 5678
node = Server()
loop.run_until_complete(node.listen(5678))
2014-01-04 20:27:45 +01:00
2018-01-02 19:14:25 +01:00
# 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.
loop.run_until_complete(node.bootstrap([("123.123.123.123", 5678)]))
2014-01-04 20:27:45 +01:00
2018-01-02 19:14:25 +01:00
# set a value for the key "my-key" on the network
loop.run_until_complete(node.set("my-key", "my awesome value"))
2014-01-04 20:27:45 +01:00
2018-01-02 19:14:25 +01:00
# get the value associated with "my-key" from the network
result = loop.run_until_complete(node.get("my-key"))
print(result)
2014-01-04 20:27:45 +01:00
```
2018-01-02 19:14:25 +01:00
## 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.
2018-01-02 19:14:25 +01:00
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.
2014-01-19 20:05:26 +01:00
## Logging
This library uses the standard [Python logging library](https://docs.python.org/3/library/logging.html). To see debut output printed to STDOUT, for instance, use:
```python
import logging
2018-01-02 19:14:25 +01:00
log = logging.getLogger('kademlia')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())
```
## Running Tests
To run tests:
```
pip install -r dev-requirements.txt
python -m unittest
```
2018-11-24 16:01:27 +01:00
## Reporting Issues
Please report all issues [on github](https://github.com/bmuller/kademlia/issues).
2014-01-19 20:05:26 +01:00
## Fidelity to Original Paper
2014-01-20 04:27:12 +01:00
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).