分为两部分,一部分用来更新数据,另一部分用来响应客户端的请求。
from SocketServer import TCPServer, StreamRequestHandler from time import ctime from SocketServer import ThreadingTCPServer import traceback import threading Mutex = threading.Lock() HOST = "127.0.0.1" #HOST = "121.40.152.123" PORT_Recv = 6000 ADDR_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()
时间: 2024-10-11 16:58:36