A P2P application with Python

30.6k views Asked by At

I am trying to implement P2P in my project, but I came across a problem because of misunderstanding of this P2P.

Here is how I am implementing it:

  1. The client open the program, and sends to the server that he wants to connect it, the server adds the client into a waiting list.

  2. When there are more then two people connected to the server, the server sends them the addresses that they should connect, and removes them from the list.

How can I create a server and a client in one file? Should I create two threads - One for the client and one for the server?

If there is a better way to do this, please let me know.

1

There are 1 answers

0
AudioBubble On

I'm not exactly sure if a P2P system is necessary for the project requirements, however...

A P2P system doesn't have separate client and server applications but instead there is a single application that acts both as a client and server. In a sense, the application acts as a client because it reaches out to a server and it acts as a server because it accepts queries/commands from a client.

http://cs.berry.edu/~nhamid/p2p/framework-python.html shows how to construct a simple p2p in Python. This example makes a loop to accept connections coming in (other peers) and spawns a thread to handle the incoming connection. It also has the structure in place to send messages back to logged peers.

A big problem is how a peer will locate other peers and this is usually handled through third party, such as a dedicated "coordination"/"bootstrap" server that can point a peer to at least another peer (which from there could ask that peer for more peers). Your project might not need that considering you already know the address your peer will use to connect to another peer.

Referencing the website above, you would make a handler function and place it in self.handlers. Since that function would be passed the incoming peer's information you could make the application send back the data you wish (the address to connect to)