2014-01-19 12:21:16 -05:00
# Python Distributed Hash Table
2014-01-04 14:27:45 -05:00
[](https://travis-ci.org/bmuller/kademlia)
2014-01-02 21:39:06 -05:00
2014-01-18 12:55:34 -05:00
This library is an asynchronous Python implementation of the [Kademlia distributed hash table ](http://en.wikipedia.org/wiki/Kademlia ). It uses [Twisted]() 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/NAT ).
2014-01-19 12:21:16 -05:00
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-02 21:39:06 -05:00
## Installation
```
2014-01-04 14:27:45 -05:00
pip install kademlia
2014-01-02 21:39:06 -05:00
```
## Usage
*This assumes you have a working familiarity with Twisted.*
2014-01-04 14:27:45 -05:00
Assuming you want to connect to an existing network (run the standalone server example below if you don't have a network):
2014-01-02 21:39:06 -05:00
```python
from twisted.internet import reactor
2014-01-04 14:27:45 -05:00
from twisted.python import log
from kademlia.network import Server
import sys
# log to std out
log.startLogging(sys.stdout)
def quit(result):
print "Key result:", result
reactor.stop()
def get(result, server):
2014-01-18 12:55:34 -05:00
return server.get("a key").addCallback(quit)
2014-01-04 14:27:45 -05:00
def done(found, server):
log.msg("Found nodes: %s" % found)
return server.set("a key", "a value").addCallback(get, server)
2014-01-02 21:39:06 -05:00
2014-01-04 14:27:45 -05:00
server = Server()
2014-01-18 12:55:34 -05:00
# next line, or use reactor.listenUDP(5678, server.protocol)
2014-01-04 14:27:45 -05:00
server.listen(5678)
2014-01-18 12:55:34 -05:00
server.bootstrap([('127.0.0.1', 1234)]).addCallback(done, server)
2014-01-04 14:27:45 -05:00
reactor.run()
```
## Stand-alone Server
If all you want to do is run a local server, just start the example server:
```
twistd -noy server.tac
2014-01-02 21:39:06 -05:00
```
2014-01-18 12:55:34 -05:00
## Running Tests
To run tests:
```
trial kademlia
```