Creating a multithreaded server using SocketServer

2019-01-09 07:23发布

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()

3条回答
【Aperson】
2楼-- · 2019-01-09 07:50

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


server = ThreadedTCPServer(('',1520), service)
server.serve_forever()
查看更多
女痞
3楼-- · 2019-01-09 07:50

Shouldn't you loop the

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

Just a guess..

查看更多
神经病院院长
4楼-- · 2019-01-09 07:55

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.
查看更多
登录 后发表回答