Skip to main content

Python3 DHT Implementation

Project description

Copyright (c) 2015, Davide Gessa
Copyright (c) 2012, Isaac Zafuta
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.

Description: # kad.py

Python3 implementation of the Kademlia DHT data store.

Useful for distributing a key-value store in a decentralized manner.

To create a new DHT swarm, just call DHT() with the host and port that you will listen on. To join an existing DHT swarm, also provide bootstrap host and port numbers of any existing node. The nodes will discover the rest of the swarm as appropriate during usage.


## Example: A two-node DHT

```python
from kad import DHT

host1, port1 = 'localhost', 3000
dht1 = DHT(host1, port1)
host2, port2 = 'localhost', 3001
dht2 = DHT(host2, port2, seeds=[(host1, port1)])
dht1["my_key"] = [u"My", u"json-serializable", u"Object"]

print (dht2["my_key"]) # blocking get
dht2.get ("my_key", lambda data: print (data)) # threaded get
```


## Example: Persistent storage

We can use a custom storage for local data. Storage parameter must an object with __getitem__ and __setitem__.
In this example we use shelve to create a persistent storage.

```python
from kad import DHT
import shelve

host, port = 'localhost', 3000
dht = DHT(host, port, storage=shelve.open ('sto.dat'))
```


## Example: Custom hash function

By default, kad.py doesn't hash keys. We can provide a custom hash_function.

```python
from kad import DHT

host, port = 'localhost', 3000
dht = DHT(host, port, hash_function=lambda d: d[0:4])
```



## Example: Custom request handler

You can extend the default DHTRequestHandler to intercept any kind of messages.

```python
from kad import *

class CustomRequestHandler (kad.DHTRequestHandler):
def handle_store(self, message):
print (message['value'])
return super (CustomRequestHandler, self).handle_store (message)


d = DHT ('localhost', 3030, requesthandler=CustomRequestHandler)

d['ciao'] = {'hola': 12}
```


## Example: Iterate over DHT keys

You can use the DHT object as iterator for stored keys.

```python
from kad import DHT

d = DHT ('localhost', 3100)

d['ciao'] = 'mondo'
d['hello'] = 'world'

for key in d:
print (key, d[key])
```
Platform: UNKNOWN

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kad.py-0.5.6.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distributions

kad.py-0.5.6-py3.4.egg (16.9 kB view hashes)

Uploaded Source

kad.py-0.5.6-py3-none-any.whl (26.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page