I was required to implement a peer-to-peer application as one of my projects for my CSC 573 – Networking course. We had the choice of implementing in Java, C, or Python. My partner and I chose Python.
The p2p app is an implementation of a simple p2p system with a centralized index (CI) at a server. Peers may share and download Request for Comments (RFC) files from one another. The CI houses the address and the list of all known RFCS the peer has.
The server runs on a local machine. It waits for connections and creates a new thread (using python _thread library) for each connection.
A client may be run from any machine. Once run, it prompts the user for the server address. Once entered, the client creates a socket connection with the server.
Each client (peer) has 3 command options:
- ADD : Adds an RFC specified by the client (if it exists). The server updates the centralized index
- LOOKUP : Lookup an RFC number to see the list of peers that have it
- LIST : List all files in the CI along with their location (peer’s IP address and port number)
- GET : Get’s the RFC from the peer
The server’s CI maintans the RFC number, RFC Title, host name, and port number of each peer so long as the peer remains connected to the server. Once a peer drops, the information is removed from the CI. This means that peers can only connect to peers that are connected to the server.
We used a list of dictionaries to store the key, value pairs for each client. Here is the server function that is called once a peer sends the ADD command.
# Prints the list of dictionary items on the CI def print_dictionary(dictionary_list, keys): for item in dictionary_list: print(' '.join([item[key] for key in keys]))
Once a peer decides to execute a GET command, the peer connects to another peer using a different socket connection. A download of the RFC occurs from the peer. We used the pickle library to load and dump the RFC.txt file from one peer to another.
This application could easily be modified and expanded to support a more general file type, rather than just RFCs.
The project worked as expected and we received full marks. We could connect many clients to one another simultaneously and share the RFCs in a network of clients. Overall, this turned out to be a useful exercise.
The full code can be seen here: https://github.com/adamgillfillan/p2p