diff --git a/README.md b/README.md index 581009d..47b80c2 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,15 @@ Assuming you want to connect to an existing network: import asyncio from kademlia.network import Server +loop = asyncio.get_event_loop() + # Create a node and start listening on port 5678 node = Server() -node.listen(5678) +loop.run_until_complete(node.listen(5678)) # Bootstrap the node by connecting to other known nodes, in this case # replace 123.123.123.123 with the IP of another node and optionally # give as many ip/port combos as you can for other nodes. -loop = asyncio.get_event_loop() loop.run_until_complete(node.bootstrap([("123.123.123.123", 5678)])) # set a value for the key "my-key" on the network diff --git a/examples/first_node.py b/examples/first_node.py index 2800f0d..3f99b67 100644 --- a/examples/first_node.py +++ b/examples/first_node.py @@ -10,12 +10,13 @@ log = logging.getLogger('kademlia') log.addHandler(handler) log.setLevel(logging.DEBUG) -server = Server() -server.listen(8468) loop = asyncio.get_event_loop() loop.set_debug(True) +server = Server() +loop.run_until_complete(server.listen(8468)) + try: loop.run_forever() except KeyboardInterrupt: diff --git a/examples/get.py b/examples/get.py index b98c5b4..ef6f56f 100644 --- a/examples/get.py +++ b/examples/get.py @@ -19,7 +19,7 @@ loop = asyncio.get_event_loop() loop.set_debug(True) server = Server() -server.listen(8469) +loop.run_until_complete(server.listen(8469)) 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])) diff --git a/examples/set.py b/examples/set.py index 1b2aae2..22160fb 100644 --- a/examples/set.py +++ b/examples/set.py @@ -19,7 +19,7 @@ loop = asyncio.get_event_loop() loop.set_debug(True) server = Server() -server.listen(8469) +loop.run_until_complete(server.listen(8469)) 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])) diff --git a/kademlia/network.py b/kademlia/network.py index 106806b..fff17ff 100644 --- a/kademlia/network.py +++ b/kademlia/network.py @@ -58,7 +58,7 @@ class Server: def _create_protocol(self): return self.protocol_class(self.node, self.storage, self.ksize) - def listen(self, port, interface='0.0.0.0'): + async def listen(self, port, interface='0.0.0.0'): """ Start listening on the given port. @@ -69,7 +69,7 @@ class Server: local_addr=(interface, port)) log.info("Node %i listening on %s:%i", self.node.long_id, interface, port) - self.transport, self.protocol = loop.run_until_complete(listen) + self.transport, self.protocol = await listen # finally, schedule refreshing table self.refresh_table() diff --git a/kademlia/tests/test_server.py b/kademlia/tests/test_server.py index 71de5e8..e2c39d2 100644 --- a/kademlia/tests/test_server.py +++ b/kademlia/tests/test_server.py @@ -1,4 +1,5 @@ import unittest +import asyncio from kademlia.network import Server from kademlia.protocol import KademliaProtocol @@ -12,9 +13,10 @@ class SwappableProtocolTests(unittest.TestCase): have a KademliaProtocol object as its protocol after its listen() method is called. """ + loop = asyncio.get_event_loop() server = Server() self.assertIsNone(server.protocol) - server.listen(8469) + loop.run_until_complete(server.listen(8469)) self.assertIsInstance(server.protocol, KademliaProtocol) server.stop() @@ -33,13 +35,14 @@ class SwappableProtocolTests(unittest.TestCase): protocol_class = CoconutProtocol # An ordinary server does NOT have a CoconutProtocol as its protocol... + loop = asyncio.get_event_loop() server = Server() - server.listen(8469) + loop.run_until_complete(server.listen(8469)) self.assertNotIsInstance(server.protocol, CoconutProtocol) server.stop() # ...but our custom server does. husk_server = HuskServer() - husk_server.listen(8469) + loop.run_until_complete(husk_server.listen(8469)) self.assertIsInstance(husk_server.protocol, CoconutProtocol) husk_server.stop()