commit 5f0a7555f821a13dc48f9bb22d1e21981280e7dc Author: murmur Date: Fri Mar 10 09:34:44 2023 +0800 tcp初始化 diff --git a/server.py b/server.py new file mode 100644 index 0000000..fe7a7d7 --- /dev/null +++ b/server.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +#coding:utf-8 +import socketserver +import subprocess + +p ={} +#保存终端信息,ccid与地址对 +#p={ccid=addr,} +#缓存未能正常发送消息的客户端 +cache=[] + +def pairs(addr,ccid): + try: + p[ccid]#没有匹配客户端 + except Exception: + p[ccid] = addr#添加或更新客户端 + for i in p: + print(i,"-",p[i]) + # return + +class MyServer(socketserver.BaseRequestHandler): + def handle(self):#回调 + print ("终端已上线:",self.client_address) + conn = self.request + offs = 6 #偏移量 + + + while True: + data = conn.recv(200) + pairs(conn,data[7+offs:11+offs]) + if not data: + break + print ("接收到新数据",self.client_address,",长度",len(data),":\r\n", data.hex(" ")) + #ack_msg = "got from "+ str(ip) + " to " + str(self.client_address) + data + conn.send(data) + +if __name__ == '__main__': + server = socketserver.ThreadingTCPServer(('localhost',8005), MyServer) + ip, port = server.server_address + print ("服务端已建立:",ip, port) + server.serve_forever() +