TCP/IP Client开发
在com.zxy.common.Com_Para.py中添加如下内容
#socket链接的自动链接时间,定时清除无效tcp连接
dSockList = {}
#本机作为客户端连接socket list
dClientThreadList = {}
#作为客户端接收数据拦截器
ClientREFLECT_IN_CLASS = "com.plugins.usereflect.testClientReflectClass1"
新建接收数据解析类com.zxy.business.Analyse_Data.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
import importlib
from com.zxy.common import Com_Para
from com.zxy.common.Com_Fun import Com_Fun
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--接收数据解析
class Analyse_Data(z_debug):
attTemChildNode = ""
def __init__(self):
pass
#接收到数据包iServ是否作为服务端接收到数据
def SubAnalyseRecBytes(self,inputStrResult, inputMServSocket, inputIServ, inputStrIP,inputStrPort):
#作为客户端接收数据拦截器
if not inputIServ and Com_Para.ClientREFLECT_IN_CLASS != "":
try:
objC = importlib.import_module(Com_Para.ClientREFLECT_IN_CLASS) #对模块进行导入
objName = Com_Para.ClientREFLECT_IN_CLASS.split(".")
objN = getattr(objC,objName[len(objName) - 1])
if hasattr(objN,"strResult"):
setattr(objN,"strResult",inputStrResult)
setattr(objN,"strIP",inputStrIP)
setattr(objN,"strPort",inputStrPort)
fun_us = getattr(objN,"init_start")
fun_us(objN)
temResult = getattr(objN,"strResult")
temSend = getattr(objN,"strSend")
temContinue = getattr(objN,"strContinue")
#发送到服务端数据
if temSend != "":
Com_Fun.SendSocket(temSend,inputMServSocket)
#不继续执行操作
if temContinue == "0":
return ""
except Exception as e:
if str(type(self)) == "<class 'type'>":
self.debug_in(self,Com_Para.ClientREFLECT_IN_CLASS+"=>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(Com_Para.ClientREFLECT_IN_CLASS+"=>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
Pass
在com.zxy.common.Com_Fun.py中添加代码
@staticmethod
def SendSocket(inputValue, inputSc):
return inputSc.send(inputValue.encode(Com_Para.U_CODE))
新建TCP/IP Client类com.zxy.tcp.TcpClient.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
import time
from com.zxy.z_debug import z_debug
from com.zxy.adminlog.UsAdmin_Log import UsAdmin_Log
from com.zxy.common import Com_Para
from com.zxy.common.Com_Fun import Com_Fun
from com.zxy.business.Analyse_Data import Analyse_Data
#监测数据采集物联网应用--TCP/IP Client
class TcpClient(z_debug):
def __init__(self, skey):
self.skey = skey
pass
def client_link(self):
sock = Com_Fun.GetHashTableNone(Com_Para.dClientThreadList,self.skey)
while True:
temInit_msg = b''#初始化流
try:
temInit_msg = sock.recv(1024*50)
temValue = temInit_msg.decode(Com_Para.U_CODE)
if temValue.replace("\r","").replace("\n","") == "Exit":
Com_Fun.RemoveHashTable(Com_Para.dClientThreadList,self.skey)
if sock is not None:
sock.close()
uL = UsAdmin_Log(Com_Para.ApplicationPath, Com_Fun.GetTime("%Y-%m-%d %H:%M:%S") + "1 close server","NetWork")
uL.WriteLog()
break
elif len(temValue) > 0:
ad = Analyse_Data()
#解析作为客户端收到数据
ad.SubAnalyseRecBytes(temValue,sock,False, sock.getpeername()[0],str(sock.getpeername()[1]))
elif len(temInit_msg) == 0:
Com_Fun.RemoveHashTable(Com_Para.dClientThreadList,self.skey)
if sock is not None:
sock.close()
uL = UsAdmin_Log(Com_Para.ApplicationPath, Com_Fun.GetTime("%Y-%m-%d %H:%M:%S") + "2 close server","NetWork")
uL.WriteLog()
break
time.sleep(0.1)
except Exception as e:
Com_Fun.RemoveHashTable(Com_Para.dClientThreadList,self.skey)
if sock is not None:
sock.close()
temLog = ""
if str(type(self)) == "<class 'type'>":
temLog = self.debug_info(self)+repr(e)
else:
temLog = self.debug_info()+repr(e)
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog+"=>"+str(e.__traceback__.tb_lineno))
uL.WriteLog()
break