2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-09 13:10:44 +00:00
Comrad/p2p/p2p.py

67 lines
1.7 KiB
Python
Raw Normal View History

2020-08-16 14:59:36 +00:00
import logging
import asyncio
2020-08-18 09:01:22 +00:00
2020-08-16 14:59:36 +00:00
import shelve
from collections import OrderedDict
import pickle,os
2020-08-17 10:36:04 +00:00
NODES_PRIME = [("128.232.229.63",8468), ("68.66.241.111",8468)]
2020-08-17 10:27:11 +00:00
2020-08-17 16:23:40 +00:00
2020-08-16 14:59:36 +00:00
2020-08-18 09:01:22 +00:00
def start_first_node(port=8468):
2020-08-17 16:23:40 +00:00
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
2020-08-17 20:59:53 +00:00
log.setLevel(logging.INFO)
2020-08-17 16:23:40 +00:00
2020-08-17 10:27:11 +00:00
loop = asyncio.get_event_loop()
loop.set_debug(True)
2020-08-17 20:59:53 +00:00
# shelf = HalfForgetfulStorage()
shelf = None
2020-08-17 10:27:11 +00:00
2020-08-18 09:01:22 +00:00
#server = Server(storage=shelf)
from kad import KadServer,HalfForgetfulStorage
server = KadServer(storage=HalfForgetfulStorage())
loop.run_until_complete(server.listen(port))
2020-08-17 10:27:11 +00:00
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.stop()
loop.close()
2020-08-18 09:01:22 +00:00
def start_second_node(port=8467):
start_first_node(port=port)
2020-08-17 10:27:11 +00:00
2020-08-18 09:01:22 +00:00
# def connect():
# # host="0.0.0.0"
2020-08-16 14:59:36 +00:00
2020-08-18 09:01:22 +00:00
# async def run():
# # Create a node and start listening on port 5678
# node = Server(storage=HalfForgetfulStorage())
# await node.listen(8469)
2020-08-16 14:59:36 +00:00
2020-08-18 09:01:22 +00:00
# # 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.
# await node.bootstrap(NODES_PRIME)
2020-08-16 14:59:36 +00:00
2020-08-18 09:01:22 +00:00
# # set a value for the key "my-key" on the network
# #await node.set("my-key2", "my awesome value")
2020-08-16 14:59:36 +00:00
2020-08-18 09:01:22 +00:00
# # get the value associated with "my-key" from the network
# #result = await node.get("my-key2")
# # print(result)
# return node
2020-08-16 14:59:36 +00:00
2020-08-18 09:01:22 +00:00
# return asyncio.run(run())