Establishing a p2p connection in python

Ville Vainio ville at
Fri Oct 8 18:33:00 CEST 2004

>>>>> "gs" == gs  <gs.lists at> writes:

    gs> Proposed solution: I thought that one approach could be to
    gs> write a small server for my public server with the sole
    gs> purpose of keeping track of connected users and establishing
    gs> p2p connections on demand.

One lightweight approach (assuming that a web server is available)
would be to put up a cgi script that adds the ip:port information to
onlineusers.txt. Then the clients can just use urllib to notify the
script when they become online, and also get the list of connected
clients by just retrieving onlineusers.txt. Of course onlineusers.txt
could contain the lists of shared files for each client as well...

If you use unique user ids, you can keep the file from growing up too
much. If not, you could clean up dead clients by trying to connect all
the specified addresses.

    gs> 1) I've written a threaded server that stores information
    gs> about connected servers in a "container class". This class
    gs> holds information about username, a unique ID, the connecting
    gs> adress and the actual socket. I have not been able to find any
    gs> information online on how i could go about to connect two
    gs> sockets. It would seem like the socket is already "occupied"
    gs> since it is connected to the server. Could one duplicate, or
    gs> otherwise get a dedicated socket for sending binary data, to
    gs> one connected socket from another? A few pointers and/or tips
    gs> would be greatly appreciated!

If you went on to "connect" the two sockets, you would just
instantiate a thread that forwards the data, and would use more server
bandwidth than an FTP server would have used.

The idea in p2p is to make clients connect to each other. The task of
the server is to tell the clients about other clients so they can
connect to each other.

    gs> 2) I've also failed to find python specific information on how
    gs> abouts one would do file resuming. I would think that you,
    gs> somehow, use md5-checksums to check file status and somehow
    gs> skip the first part of the datastream. Also, pointers and
    gs> maybe an explanation in (short) pseudo-code would be much
    gs> appreciated.

It's trivial since you are going to implement a custom protocol
anyway. Make the "get file" command that you pass to the file provider
such that in addition to the file name you pass the starting offset
within the file.

    gs> 3) Is this too big of a project for a novice programmer such
    gs> as myself? I do want to go through with this since it's a
    gs> genuine problem, but i do not know if i'm ready. I'm not a
    gs> "quitter" per say, but i'm afraid doing something TOO advanced
    gs> in the beginning might be overwhelming :)

No, it's a great first project. Go ahead, I believe you will find it
surprisingly easy, educational and something for which Python is a
perfect fit.

It'll be a fun project to expand as well, to provide chunked file
transfer etc. You could also explore the alternative of making it a
bunch of wrapper scripts that just invoke bittorrent...

Ville Vainio

More information about the Python-list mailing list