mirror of https://github.com/bmuller/kademlia.git
fixed examples
This commit is contained in:
parent
20dd256608
commit
f3641864d0
|
@ -7,13 +7,13 @@ Kademlia Documentation
|
|||
======================
|
||||
|
||||
.. note ::
|
||||
This library assumes you have a working familiarity with Twisted_.
|
||||
This library assumes you have a working familiarity with asyncio_.
|
||||
|
||||
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>`_.
|
||||
This library is an asynchronous Python implementation of the `Kademlia distributed hash table <http://en.wikipedia.org/wiki/Kademlia>`_. It uses asyncio_ 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>`_.
|
||||
|
||||
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.
|
||||
|
||||
.. _Twisted: https://twistedmatrix.com
|
||||
.. _asyncio: https://docs.python.org/3/library/asyncio.html
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 3
|
||||
|
|
|
@ -8,29 +8,25 @@ The easiest (and best) way to install kademlia is through `pip <http://www.pip-i
|
|||
|
||||
Usage
|
||||
=====
|
||||
Assuming you want to connect to an existing network (run the `Stand-alone Server`_ example below if you don't have a network):
|
||||
To start a new network, create the first node. Future nodes will connect to this first node (and any other nodes you know about) to create the network.
|
||||
|
||||
.. literalinclude:: ../examples/example.py
|
||||
.. literalinclude:: ../examples/first_node.py
|
||||
|
||||
Check out the examples folder for other examples.
|
||||
Here's an example of bootstrapping a new node against a known node and then setting a value:
|
||||
|
||||
.. literalinclude:: ../examples/set.py
|
||||
|
||||
.. note ::
|
||||
You must have at least two nodes running to store values. If a node tries to store a value and there are no other nodes to provide redundancy, then it is an exception state.
|
||||
|
||||
Stand-alone Server
|
||||
==================
|
||||
|
||||
If all you want to do is run a local server, just start the example server::
|
||||
|
||||
$ twistd -noy examples/server.tac
|
||||
|
||||
|
||||
Running Tests
|
||||
=============
|
||||
|
||||
To run tests::
|
||||
|
||||
$ trial kademlia
|
||||
$ pip install -r dev-requirements.txt
|
||||
$ python -m unittest
|
||||
|
||||
|
||||
Fidelity to Original Paper
|
||||
|
|
|
@ -3,10 +3,10 @@ Querying the DHT
|
|||
|
||||
If you just want to query the network, you can use the example query script. For instance::
|
||||
|
||||
$ python examples/query.py 1.2.3.4 8468 SpecialKey
|
||||
$ python examples/get.py 1.2.3.4 8468 SpecialKey
|
||||
|
||||
The query script is simple:
|
||||
|
||||
.. literalinclude:: ../examples/query.py
|
||||
.. literalinclude:: ../examples/get.py
|
||||
|
||||
Check out the examples folder for other examples.
|
||||
|
|
|
@ -4,8 +4,8 @@ import sys
|
|||
|
||||
from kademlia.network import Server
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python get.py <key>")
|
||||
if len(sys.argv) != 4:
|
||||
print("Usage: python get.py <bootstrap node> <bootstrap port> <key>")
|
||||
sys.exit(1)
|
||||
|
||||
handler = logging.StreamHandler()
|
||||
|
@ -20,8 +20,9 @@ loop.set_debug(True)
|
|||
|
||||
server = Server()
|
||||
server.listen(8469)
|
||||
loop.run_until_complete(server.bootstrap([("127.0.0.1", 8468)]))
|
||||
result = loop.run_until_complete(server.get(sys.argv[1]))
|
||||
bootstrap_node = (sys.argv[1], int(sys.argv[2]))
|
||||
loop.run_until_complete(server.bootstrap([bootstrap_node]))
|
||||
result = loop.run_until_complete(server.get(sys.argv[3]))
|
||||
server.stop()
|
||||
loop.close()
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@ import sys
|
|||
|
||||
from kademlia.network import Server
|
||||
|
||||
if len(sys.argv) != 3:
|
||||
print("Usage: python set.py <key> <value>")
|
||||
if len(sys.argv) != 5:
|
||||
print("Usage: python set.py <bootstrap node> <bootstrap port> <key> <value>")
|
||||
sys.exit(1)
|
||||
|
||||
handler = logging.StreamHandler()
|
||||
|
@ -20,7 +20,8 @@ loop.set_debug(True)
|
|||
|
||||
server = Server()
|
||||
server.listen(8469)
|
||||
loop.run_until_complete(server.bootstrap([("127.0.0.1", 8468)]))
|
||||
loop.run_until_complete(server.set(sys.argv[1], sys.argv[2]))
|
||||
bootstrap_node = (sys.argv[1], int(sys.argv[2]))
|
||||
loop.run_until_complete(server.bootstrap([bootstrap_node]))
|
||||
loop.run_until_complete(server.set(sys.argv[3], sys.argv[4]))
|
||||
server.stop()
|
||||
loop.close()
|
||||
|
|
Loading…
Reference in New Issue