I am very lost on this assignment. We are supposed to use kademlia to create 3 python files. The 2 files are nodes and then the third are a set a query the DHT application. We are also given a CSV file that we must import and and send the data to the DHT network. Any help would be greatly appreciated!!! Here is the assignment:
You will first need to create an initial starting node (see code below). This will start on your local system on port 8468.
# Modified from: https://kademlia.readthedocs.io/en/latest/intro.html
import logging
import asyncio
from kademlia.network import Server
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)
loop = asyncio.get_event_loop()
loop.set_debug(True)
server = Server()
loop.run_until_complete(server.listen(8468))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.stop()
loop.close()
Next, you will need to start another local node of the DHT application on port 8469 (see code below).
# Modified from: https://kademlia.readthedocs.io/en/latest/intro.html
import logging
import asyncio
from kademlia.network import Server
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)
bootstrap_node = ("localhost", int("8468"))
loop = asyncio.get_event_loop()
loop.set_debug(True)
server = Server()
loop.run_until_complete(server.listen(8469))
loop.run_until_complete(server.bootstrap([bootstrap_node]))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.stop()
loop.close()
Finally, the follow code will allow you to query the DHT application and add key/value pairs (see below).
# Modified from: https://kademlia.readthedocs.io/en/latest/querying.html
import logging
import asyncio
from kademlia.network import Server
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)
async def run():
server = Server()
await server.listen(8470)
bootstrap_node = ("localhost", int("8469"))
await server.bootstrap([bootstrap_node])
await server.set("myKey2", "myValue2")
result = await server.get("myKey2")
print("Get result:", result)
server.stop()
asyncio.run(run())
Now that you have the code for two nodes and the code to set and query the DHT application, using the DHT_Data.csv file, write a Python program that imports the CSV file and sends that data to the DHT network. Additionally, write some code that will take a list of the keys in the DHT_Data.csv file and query the DHT application to show that you can query and return the values.
Here is a screenshot of the CSV file: DHT_Data.csv
Here are Screenshots of my code: