This question is solved. The working code is given below in this post too.
Problem: I am currently getting familiar with network programming with Python. I am currently working with SocketServer framework. Now my question is how can I create multi-threaded server that can accept more than one client using SocketServer module? I basically tried to use this code
t = Thread(target=server.serve_forever())
t.start()
Currently in my programme the server accepts only one client. I connect to server using netcat. The first client connects to server without any problems. If I try to connect to server from second client the client just keeps waiting to get connected. As soon as I disconnect the first client, the second client connects with the server automatically. It seems to me that the multi-threading is not working. I can't figure out where am I missing something. Any hint will be greatful. My code is as below:
#!/usr/bin/env python
import SocketServer
from threading import Thread
class service(SocketServer.BaseRequestHandler):
def handle(self):
data = 'dummy'
print "Client connected with ", self.client_address
while len(data):
data = self.request.recv(1024)
self.request.send(data)
print "Client exited"
self.request.close()
server = SocketServer.TCPServer(('',1520), service)
t = Thread(target=server.serve_forever())
t.start()
Thanks
Update: The following code is the solution:
#!/usr/bin/env python
import SocketServer
from threading import Thread
class service(SocketServer.BaseRequestHandler):
def handle(self):
data = 'dummy'
print "Client connected with ", self.client_address
while len(data):
data = self.request.recv(1024)
self.request.send(data)
print "Client exited"
self.request.close()
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
t = ThreadedTCPServer(('',1520), service)
t.serve_forever()