fixed examples

This commit is contained in:
Brian Muller 2018-01-02 14:17:36 -05:00
parent 20dd256608
commit f3641864d0
5 changed files with 22 additions and 24 deletions

View File

@ -7,13 +7,13 @@ Kademlia Documentation
====================== ======================
.. note :: .. 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. 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:: .. toctree::
:maxdepth: 3 :maxdepth: 3

View File

@ -8,29 +8,25 @@ The easiest (and best) way to install kademlia is through `pip <http://www.pip-i
Usage 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 :: .. 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. 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 Running Tests
============= =============
To run tests:: To run tests::
$ trial kademlia $ pip install -r dev-requirements.txt
$ python -m unittest
Fidelity to Original Paper Fidelity to Original Paper

View File

@ -3,10 +3,10 @@ Querying the DHT
If you just want to query the network, you can use the example query script. For instance:: 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: The query script is simple:
.. literalinclude:: ../examples/query.py .. literalinclude:: ../examples/get.py
Check out the examples folder for other examples. Check out the examples folder for other examples.

View File

@ -4,8 +4,8 @@ import sys
from kademlia.network import Server from kademlia.network import Server
if len(sys.argv) != 2: if len(sys.argv) != 4:
print("Usage: python get.py <key>") print("Usage: python get.py <bootstrap node> <bootstrap port> <key>")
sys.exit(1) sys.exit(1)
handler = logging.StreamHandler() handler = logging.StreamHandler()
@ -20,8 +20,9 @@ loop.set_debug(True)
server = Server() server = Server()
server.listen(8469) server.listen(8469)
loop.run_until_complete(server.bootstrap([("127.0.0.1", 8468)])) bootstrap_node = (sys.argv[1], int(sys.argv[2]))
result = loop.run_until_complete(server.get(sys.argv[1])) loop.run_until_complete(server.bootstrap([bootstrap_node]))
result = loop.run_until_complete(server.get(sys.argv[3]))
server.stop() server.stop()
loop.close() loop.close()

View File

@ -4,8 +4,8 @@ import sys
from kademlia.network import Server from kademlia.network import Server
if len(sys.argv) != 3: if len(sys.argv) != 5:
print("Usage: python set.py <key> <value>") print("Usage: python set.py <bootstrap node> <bootstrap port> <key> <value>")
sys.exit(1) sys.exit(1)
handler = logging.StreamHandler() handler = logging.StreamHandler()
@ -20,7 +20,8 @@ loop.set_debug(True)
server = Server() server = Server()
server.listen(8469) server.listen(8469)
loop.run_until_complete(server.bootstrap([("127.0.0.1", 8468)])) bootstrap_node = (sys.argv[1], int(sys.argv[2]))
loop.run_until_complete(server.set(sys.argv[1], 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() server.stop()
loop.close() loop.close()