Search code examples
pythonsocketssocketserver

Can I use the Python SocketServer class for this implementation?


I've come to the realization where I need to change my design for a file synchronization program I am writing.

Currently, my program goes as follows:

1) client connects to server (and is verified)

2) if the client is verified, create a thread and begin a loop using the socket the client connected with

3) if a file on the client or server changes, send the change through that socket (using select for asynchronous communication)

My code sucks because I am torn between using one socket for file transfer or using a socket for each file transfer. Either case (in my opinion) will work, but for the first case I would have to create some sort of protocol to determine what bytes go where (some sort of header), and for the second case, I would have to create new sockets on a new thread (that do not need to be verified again), so that files can be sent on each thread without worrying about asynchronous transfer.

I would prefer to do the second option, so I'm investigating using SocketServer. Would this kind of problem be solved with SocketServer.ThreadingTCPServer and SocketServer.ThreadingMixIn? I'm having trouble thinking about it because I would assume SocketServer.ThreadingMixIn works for newly connected clients, unless I somehow have an "outer" socket server which servers "inner" socket servers?


Solution

  • SocketServer will work for you. You create one SocketServer per port you want to listen on. Your choice is whether you have one listener that handles the client/server connection plus per file connections (you'd need some sort of header to tell the difference) or two listeners that separate client/server connection and per file connections (you'd still need a header so that you knew which file was coming in).

    Alternately, you could choose something like zeromq that provides a message transport for you.