GithubHelp home page GithubHelp logo

pombredanne / libdht Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flosch/libdht

0.0 1.0 0.0 132 KB

Pure python library for implementing a simple DHT based on the Kademlia protocol.

Home Page: http://www.florian-schlachter.de/post/libdht/

License: MIT License

Python 100.00%

libdht's Introduction

Important note: The library is still work in progress, there are a lot of TODOs in it. So please be careful when using libdht. I strongly advise against using it in a production environment yet.

libdht supports bootstrapping (currently only searching for nodes in the local network using broadcast).

Simple example of how to use the DHT library:

(There are already 2 other nodes started.)

$ python example.py
My ID = 315266357664053077240863549245018455720435477024

Saving: True
Loading: Hallo!
How many nodes have this key? 3
Removing: True
How many nodes have this key now? 0
Enter to exit.
# -*- coding: utf-8 -*-

import random, time, sys, dht, bootstrap
random.seed(time.time())

class MyNetwork(dht.DHT):
    def __init__(self, *args, **kwargs):
        self._my_db = {}
        super(MyNetwork, self).__init__(*args, **kwargs)

    def handle_save(self, key, value):
        self._my_db[key] = value
        return True

    def handle_load(self, key):
        return self._my_db.get(key)

    def handle_delete(self, key):
        del self._my_db[key]
        return True

    def handle_has_key(self, key):
        return key in self._my_db

def main():
    # Uses port as first argument for communication (TCP+UDP)
    my_id = random.randint(0, dht.MAX_ID)
    port = random.randint(5000, 10000)
    n = MyNetwork(node_id=my_id, port=port)
    
    bootstrapper = bootstrap.Bootstrapper(network_id="test", node_id=my_id,
                                          dht_port=port)
    bootstrapper.start_network(n)
    try:
        print "My ID = %d" % my_id
        print
     
        # Hash your data (160-bit integer), for this example we'll get a random int
        data_id = random.randint(0, dht.MAX_ID)
    
        # Returns True on success, gets automatically replicated
        print "Saving:", n.save(data_id, "Hallo!", replicas=20)
    
        # Returns "Hallo!" (received from one of the nodes available in the network having this key)
        print "Loading:", n.load(data_id)
    
        # Is the key available in the network? Returns the number of replicas.
        print "How many nodes have this key?", n.has_key(data_id)
    
        # Removes the key+data from all nodes in the network
        print "Removing:", n.delete(data_id)
        
        print "How many nodes have this key now?", n.has_key(data_id)
    
        raw_input("Enter to exit.")
    finally:
        # Make sure network is always shutting down
        bootstrapper.stop_network(n)

if __name__ == "__main__":
    main()

libdht's People

Contributors

flosch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.