From 5f0a7555f821a13dc48f9bb22d1e21981280e7dc Mon Sep 17 00:00:00 2001 From: murmur Date: Fri, 10 Mar 2023 09:34:44 +0800 Subject: [PATCH] =?UTF-8?q?tcp=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 server.py 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() +