parent
0953e0edb2
commit
be6b60847b
@ -23,14 +23,15 @@ Assuming you want to connect to an existing network:
|
|||||||
import asyncio
|
import asyncio
|
||||||
from kademlia.network import Server
|
from kademlia.network import Server
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
# Create a node and start listening on port 5678
|
# Create a node and start listening on port 5678
|
||||||
node = Server()
|
node = Server()
|
||||||
node.listen(5678)
|
loop.run_until_complete(node.listen(5678))
|
||||||
|
|
||||||
# Bootstrap the node by connecting to other known nodes, in this case
|
# 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
|
# 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.
|
# 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)]))
|
loop.run_until_complete(node.bootstrap([("123.123.123.123", 5678)]))
|
||||||
|
|
||||||
# set a value for the key "my-key" on the network
|
# set a value for the key "my-key" on the network
|
||||||
|
@ -10,12 +10,13 @@ log = logging.getLogger('kademlia')
|
|||||||
log.addHandler(handler)
|
log.addHandler(handler)
|
||||||
log.setLevel(logging.DEBUG)
|
log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
server = Server()
|
|
||||||
server.listen(8468)
|
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.set_debug(True)
|
loop.set_debug(True)
|
||||||
|
|
||||||
|
server = Server()
|
||||||
|
loop.run_until_complete(server.listen(8468))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
@ -19,7 +19,7 @@ loop = asyncio.get_event_loop()
|
|||||||
loop.set_debug(True)
|
loop.set_debug(True)
|
||||||
|
|
||||||
server = Server()
|
server = Server()
|
||||||
server.listen(8469)
|
loop.run_until_complete(server.listen(8469))
|
||||||
bootstrap_node = (sys.argv[1], int(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.bootstrap([bootstrap_node]))
|
||||||
result = loop.run_until_complete(server.get(sys.argv[3]))
|
result = loop.run_until_complete(server.get(sys.argv[3]))
|
||||||
|
@ -19,7 +19,7 @@ loop = asyncio.get_event_loop()
|
|||||||
loop.set_debug(True)
|
loop.set_debug(True)
|
||||||
|
|
||||||
server = Server()
|
server = Server()
|
||||||
server.listen(8469)
|
loop.run_until_complete(server.listen(8469))
|
||||||
bootstrap_node = (sys.argv[1], int(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.bootstrap([bootstrap_node]))
|
||||||
loop.run_until_complete(server.set(sys.argv[3], sys.argv[4]))
|
loop.run_until_complete(server.set(sys.argv[3], sys.argv[4]))
|
||||||
|
@ -58,7 +58,7 @@ class Server:
|
|||||||
def _create_protocol(self):
|
def _create_protocol(self):
|
||||||
return self.protocol_class(self.node, self.storage, self.ksize)
|
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.
|
Start listening on the given port.
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ class Server:
|
|||||||
local_addr=(interface, port))
|
local_addr=(interface, port))
|
||||||
log.info("Node %i listening on %s:%i",
|
log.info("Node %i listening on %s:%i",
|
||||||
self.node.long_id, interface, port)
|
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
|
# finally, schedule refreshing table
|
||||||
self.refresh_table()
|
self.refresh_table()
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from kademlia.network import Server
|
from kademlia.network import Server
|
||||||
from kademlia.protocol import KademliaProtocol
|
from kademlia.protocol import KademliaProtocol
|
||||||
@ -12,9 +13,10 @@ class SwappableProtocolTests(unittest.TestCase):
|
|||||||
have a KademliaProtocol object as its protocol after its listen()
|
have a KademliaProtocol object as its protocol after its listen()
|
||||||
method is called.
|
method is called.
|
||||||
"""
|
"""
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
server = Server()
|
server = Server()
|
||||||
self.assertIsNone(server.protocol)
|
self.assertIsNone(server.protocol)
|
||||||
server.listen(8469)
|
loop.run_until_complete(server.listen(8469))
|
||||||
self.assertIsInstance(server.protocol, KademliaProtocol)
|
self.assertIsInstance(server.protocol, KademliaProtocol)
|
||||||
server.stop()
|
server.stop()
|
||||||
|
|
||||||
@ -33,13 +35,14 @@ class SwappableProtocolTests(unittest.TestCase):
|
|||||||
protocol_class = CoconutProtocol
|
protocol_class = CoconutProtocol
|
||||||
|
|
||||||
# An ordinary server does NOT have a CoconutProtocol as its protocol...
|
# An ordinary server does NOT have a CoconutProtocol as its protocol...
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
server = Server()
|
server = Server()
|
||||||
server.listen(8469)
|
loop.run_until_complete(server.listen(8469))
|
||||||
self.assertNotIsInstance(server.protocol, CoconutProtocol)
|
self.assertNotIsInstance(server.protocol, CoconutProtocol)
|
||||||
server.stop()
|
server.stop()
|
||||||
|
|
||||||
# ...but our custom server does.
|
# ...but our custom server does.
|
||||||
husk_server = HuskServer()
|
husk_server = HuskServer()
|
||||||
husk_server.listen(8469)
|
loop.run_until_complete(husk_server.listen(8469))
|
||||||
self.assertIsInstance(husk_server.protocol, CoconutProtocol)
|
self.assertIsInstance(husk_server.protocol, CoconutProtocol)
|
||||||
husk_server.stop()
|
husk_server.stop()
|
||||||
|
Loading…
Reference in New Issue
Block a user