Skip to content

Commit

Permalink
fixed examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuller committed Jan 2, 2018
1 parent 20dd256 commit f364186
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions docs/index.rst
Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions docs/intro.rst
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/querying.rst
Expand Up @@ -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.
9 changes: 5 additions & 4 deletions examples/get.py
Expand Up @@ -4,8 +4,8 @@

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()
Expand All @@ -20,8 +20,9 @@

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()

Expand Down
9 changes: 5 additions & 4 deletions examples/set.py
Expand Up @@ -4,8 +4,8 @@

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()
Expand All @@ -20,7 +20,8 @@

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()

0 comments on commit f364186

Please sign in to comment.