执行shell命令的多线程多连接SocketServer

import socketserver
import subprocess

host = ''
port = 8000
address = (host,port)

class Socket_Server(socketserver.StreamRequestHandler):
    def handle(self):
        print('Connect by:',self.client_address)
        while True:
            data = bytes.decode(self.request.recv(1024))
            result = subprocess.check_output(data, shell=True)
            count = len(result)
            print('count:', count)
            self.request.send(bytes(str(count), encoding='utf8'))
            while count > 0:
                count2 = self.request.send(result)
                count = count - count2

print('SocktServer is running...')
server = socketserver.ThreadingTCPServer(address,Socket_Server)
server.serve_forever()

 https://my.oschina.net/duhaizhang/blog/68639

posted @ 2016-12-19 23:21  Vincen_shen  阅读(155)  评论(0)    收藏  举报