python中同时处理多个连接

YetToCome 发布于 2013/11/13 12:21
阅读 1K+
收藏 0

想用python写一个服务器,同时处理多个TCP的连接,不知道该怎么写

之前听过一种gevent的框架,不知道可不可以使用,可能需要用到多线程什么的,不过我不大清楚怎么写。

希望大家能提供点方法,谢谢了!!

加载中
0
李学明
李学明

一个非常非常简单的线程模型的echo server

from SocketServer import ThreadingTCPServer, BaseRequestHandler

class EchoHandler(BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024)
        self.request.sendall(self.data)


if __name__ == '__main__':
    HOST, PORT = "localhost", 9999
    server = ThreadingTCPServer((HOST, PORT), EchoHandler) 
    server.serve_forever()

0
zhengyijie
zhengyijie

写TCP/IP服务器的流程都差不多。用python先去看两个模块,thread ,socket.  给你贴段代码:

import threading,thread
import socket,time,sys
import Map_Client,funtion

class MapServer(threading.Thread):
    def __init__(self,DisplayList=None,MappingInforList=None):
        threading.Thread.__init__(self)
        self.DisplayList=DisplayList
        self.MappingInforList=MappingInforList
        self.MappingClientList=[]
        self.RLock=threading.RLock()
        self.ip=self.MappingInforList[0]
        self.port=int(self.MappingInforList[1])
        self.isTrue=True

    def run(self):
        self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind((self.ip,self.port))
        self.server.listen(1000)
##            print self.ip,self.port
        while self.isTrue:
            InConnection,address=self.server.accept()
            if self.isTrue:
                pass
            else:
                break
            try:
                OutConnection=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                OutConnection.connect((self.MappingInforList[2],int(self.MappingInforList[3])))
            except Exception,data:
                print data
                InConnection.close()
                continue
            InConnection.settimeout(2)
            OutConnection.settimeout(2)
            self.DisplayList[4]=str(int(self.DisplayList[4])+1)
            MapInClient=Map_Client.MapInClient(InConnection,OutConnection,self.DisplayList,self.RLock)
            MapInClient.setDaemon(True)
            MapInClient.start()
            MapOutClient=Map_Client.MapOutClient(InConnection,OutConnection,self.DisplayList,self.RLock)
            MapOutClient.setDaemon(True)
            MapOutClient.start()
            tem_list=(MapInClient,MapOutClient)
            self.MappingClientList.append(tem_list)

    def stop(self):
        try:
            self.isTrue=False
            self.server.close()
            socket_connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            socket_connect.connect((self.ip,self.port))
            socket_connect.close()
            for client in range(0,len(self.MappingClientList)):
                self.MappingClientList[client][0].stop()
                self.MappingClientList[client][1].stop()
            self.MappingClientList=[]
            self.DisplayList[4]='0'
        except Exception,data:
            print data,"close port fail"

0
mallon
mallon
看select模块文档
返回顶部
顶部