在python中使用SocketServer框架创建多线程服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20745352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:08:47  来源:igfitidea点击:

Creating a multithreaded server using SocketServer framework in python

pythonmultithreadingsockets

提问by ρss

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
sys.dont_write_bytecode = True
import shlex
import subprocess
import SocketServer

sess = []

class TCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        global sess
        sess.append(self.request)
        ip,port = self.client_address
        print "#%d: client %s:%d"%(len(sess),ip,port)
        while True:
            cmd = self.request.recv(8192)
            out = subprocess.check_output(shlex.split(cmd),stderr=subprocess.STDOUT,shell=True)
            self.request.send(out)
        self.request.close()
class ThreadedTCPServer(SocketServer.ThreadingMixIn,SocketServer.TCPServer): pass

if __name__ == "__main__":
    port = 4242
    svr = ThreadedTCPServer(("",port),TCPHandler)
    print ":%d"%port
    svr.serve_forever()

采纳答案by smeso

It is much more simple than you think:

它比你想象的要简单得多:

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

Than you just have to use your new ThreadedTCPServerinstead of TCPServer.

比你只需要使用你的 newThreadedTCPServer而不是TCPServer.

For more information you can read some doc.

有关更多信息,您可以阅读一些文档

However in your code you made some mistakes:

但是在您的代码中,您犯了一些错误:

  1. The targetargument must be a callableobject 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.
  1. target参数必须是一个callable对象不是“已调用”对象。
  2. 要处理许多请求,您需要构建一个线程池。如果您只使用一个线程,那么它是主线程还是“子”线程没有任何区别。

回答by Aviram

Shouldn't you loop the

你不应该循环

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

Just a guess..

只是猜测..

回答by ρss

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys
sys.dont_write_bytecode = True
import shlex
import subprocess
import SocketServer

sess = []

class TCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        global sess
        sess.append(self.request)
        ip,port = self.client_address
        print "#%d: client %s:%d"%(len(sess),ip,port)
        while True:
            cmd = self.request.recv(8192)
            out = subprocess.check_output(shlex.split(cmd),stderr=subprocess.STDOUT,shell=True)
            self.request.send(out)
        self.request.close()
class ThreadedTCPServer(SocketServer.ThreadingMixIn,SocketServer.TCPServer): pass

if __name__ == "__main__":
    port = 4242
    svr = ThreadedTCPServer(("",port),TCPHandler)
    print ":%d"%port
    svr.serve_forever()