Example #1
0
    def __init__(self, address, port, node_id=None, bootstrap=[]):
        """
        Creates a new peer, with the given settings.

        Does not bind a socket yet. Call `serve_forever` to bind and listen.

        >>> PeerServer('', '9000').serve_forever()

        :param address: IP address to bind to
        :param port: Port to bind to
        :param node_id: Optional Node identifier, if this
                   peer already has a persisted identity.
        :param bootstrap: A list of (ip, port) tuples used
                          as an entry point into the network.
        """
        super().__init__('%s:%s' % (address, port))

        self._id = NodeId.generate() if node_id is None else node_id
        self._bootstrap = list(bootstrap)
        print('Starting Peer %s' % repr(self._id))
Example #2
0
import gevent

from dht.peer import PeerServer
from dht.route import NodeId


if __name__ == '__main__':
    print('Starting up')

    id = NodeId.generate()

    # Stop gevent from outputting exception
    # details on keyboard interrupt.
    gevent.get_hub().NOT_ERROR += (KeyboardInterrupt,)

    peer = PeerServer('', '9000')
    cancel = gevent.event.Event()

    try:
        peer.start()
        print('Bootstrap here')
        cancel.wait()
    except KeyboardInterrupt:
        print('User requested stop')
    finally:
        print('Stopping')
        peer.stop(timeout=1)

        # Currently this doesn't do anything, as the
        # cancel event is just used to stop main() execution.