43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
#!/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()
|
||
|