Which way is better?
Creating a while loop and then using the select module OR using ThreadedTCPServer with a custom class.
Im having problems with the Threaded TCP Server, although it could just be my coding.
-
Better by what criteria?Matt Ball– Matt Ball2011年07月14日 21:08:13 +00:00Commented Jul 14, 2011 at 21:08
-
Well, what is the general method.Anonymous– Anonymous2011年07月14日 21:08:28 +00:00Commented Jul 14, 2011 at 21:08
-
I thought while loops are bad, but then again it seems to be working better than the "python approved" method.Anonymous– Anonymous2011年07月14日 21:09:02 +00:00Commented Jul 14, 2011 at 21:09
-
What kind of server? HTTP/web, or lower-level TCP? You're almost certainly best off not writing your own, because you'll be debugging problems others have already found solutions for. What kinds of problems are you having with ThreadedTCPServer?Ben Hoyt– Ben Hoyt2011年07月14日 21:27:34 +00:00Commented Jul 14, 2011 at 21:27
-
Very slow, sluggish, when i do a keyboardinterrupt it will hang for anywhere up to 5 minutes (there are known problems, that i found on google with this apparently) Also forgot to say, low level TCPAnonymous– Anonymous2011年07月14日 21:45:10 +00:00Commented Jul 14, 2011 at 21:45
2 Answers 2
My personal recommendation is to use Twisted. It's a Python-based framework intended primarily for writing event-driven network software. The documentation has a lot of great examples of how to create various types of servers and clients, as well.
1 Comment
I am sure there is no such a thing like the "correct" way.
If you want not, must not or cannot use any of the existing server implementations the general idea is (in pseudo code):
ss = serversocket()
ss.bind ()
while (True):
cs = ss.accept ()
spawnCommThread (cs)
In the CommThread for each client you take care of reading from the socket returned by accept, communicate with your client and die, when the client closes the connection or another criterion is given.