You need to call serve_forever()
to get your SocketServer
object to start listening for connections. The serve_forever()
method, as its name implies, will never return. Given this fact, you probably want to call it in your thread in order to allow the rest of your code to proceed. For example:
class MyThreads(threading.Thread):
def __init__(self):
self.server = None;
threading.Thread.__init__(self);
def run(self):
if self.server == None:
address = ('localhost', 40000);
self.server = SocketServer.TCPServer(address, EchoRequestHandler);
self.server.serve_forever()
You may run into race conditions here if your client code attempts to connect before the socket is listening. Possibly time.sleep(...)
will help you here.