Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question is solved. The working code is given below in this post too. 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

ThreadedTCPServer(('',1520), service)
serve_forever()
share|improve this question

3 Answers 3

up vote 3 down vote accepted

It is much more simple than you think:

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

Than you just have to use your new ThreadedTCPServer instead of TCPServer.

For more information you can read some doc.

However in your code you made some mistakes:

  1. The target argument must be a callable object not an "already-called" object.
  2. To handle many requests you need to build a Threads pool. If you only use one thread it does not make any difference if it is the main thread or a "child" thread.
share|improve this answer
    
Well, thanks. The example in docs is complicated for newbie like me. So, I used your idea and now my programme works as intended. –  ρss Dec 23 '13 at 15:18

Shouldn't you loop the

server = SocketServer.TCPServer(('',1520), service)
t = Thread(target=server.serve_forever())
t.start()

Just a guess..

share|improve this answer

Well thanks to @Faust for the suggestion. The code given below now works as it was intended.

#!/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

ThreadedTCPServer(('',1520), service)
serve_forever()
share|improve this answer
1  
This line thread.start_new_thread(server.serve_forever(),()) should simply be server.serve_forever(). –  Faust Dec 23 '13 at 15:27
    
@Faust thanks I edited the code. serve_forever() works too. –  ρss Dec 23 '13 at 20:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.