PYthon multi-thread server and python multi-thread
It is divided into two parts, one for updating data and the other for responding to client requests.
from SocketServer import TCPServer, StreamRequestHandlerfrom time import ctimefrom SocketServer import ThreadingTCPServerimport tracebackimport threadingMutex = threading.Lock()HOST = "127.0.0.1"#HOST = "121.40.152.123"PORT_Recv = 6000ADDR_Recv = (HOST,PORT_Recv)Int = [ i for i in range (101)]dict_ele = {'001':'100'} for i in range(101): Key_ele = str(i) if len(Key_ele) == 1: Key_ele = '00' + Key_ele dict_ele.update({Key_ele:"100"}) elif len(Key_ele) == 2: Key_ele = '0' + Key_ele dict_ele.update({Key_ele:"100"}) else : dict_ele.update({Key_ele:"100"})class Recv_Data_from_Vehicle_RequestHandle(StreamRequestHandler): def handle(self): #data = self.rfile.readline() data = self.request.recv(1024) if len(data) == 12: if data[0] == '$' and data[10:12] == '\r\n' : Key = data[3:6] Value = data[7:10] if Key.isdigit(): if int(Key) <= 100 and int (Key) >= 0: Mutex.acquire() dict_ele.update({Key:Value})#need protect print Key,dict_ele[Key] Mutex.release() self.wfile.write(dict_ele[Key]) else : print " overflow" else: print "Key Error" else: print "bit error!" else: print "recv error!" def Recv_Data_from_Vehicle(): tcpServ = ThreadingTCPServer(ADDR_Recv,Recv_Data_from_Vehicle_RequestHandle) tcpServ.serve_forever()class Query_Data_RequestHandle(StreamRequestHandler): def handle(self): if 1: try: #data = "$NUM001\r\n" #print len(data) #data = self.request.recv(1024) data = self.rfile.readline() #read data index ="001" #initialize print "recv from (%r):%r"% (self.client_address,data) #print data[4:7] #print "len=:",len(data) Error_buf = '$DATA' + index + ',' + 'Err' + '\r\n' if len(data) == 9: if data[0] == '$' and data[7:9] == '\r\n': index = data[4:7] if index.isdigit(): if int(index) <= 100 and int (index) >= 0: Mutex.acquire() Send_buf = '$DATA' + index + ',' + dict_ele[index] + '\r\n'#need protect self.wfile.write(Send_buf) print "Send_buf:",Send_buf Mutex.release() else : print "index including other char" Error_buf = '$DATA' + index + ',' + 'Err' + '\r\n' self.wfile.write(Error_buf) else: print "index over" Error_buf = '$DATA' + index + ',' + 'Err' + '\r\n' self.wfile.write(Error_buf) else: print "bit error!" else: print "recv error!" # print self.request.threading.Thread.getName() # sleep(5) except: traceback.print_exc() def Query_Data(): PORT_Query = 6001 addr_Query = (HOST,PORT_Query) server = ThreadingTCPServer(addr_Query,Query_Data_RequestHandle) server.serve_forever()def main(): Recv_Thread = threading.Thread(target = Recv_Data_from_Vehicle) Query_Thread = threading.Thread(target = Query_Data) Recv_Thread.start() Query_Thread.start()if __name__ == "__main__": main()
What can I do if the server sets thread restrictions when using multiple threads to crawl a webpage?
Youdao API has limits, and the Access Frequency of the same IP address cannot be too high. You can try using another IP address or sleep for several seconds in each thread.
In python, how does a server written by tcpServer connect multiple clients through multiple threads and return the connected socket for event-driven purposes?
Serve_forever does not have event processing. You can open a thread for each socket.