- 监测数据采集物联网应用开发步骤(9.1)
TCP/IP Server开发
新建TCP/IP Server线程类com.zxy.tcp.ServerThread.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
import socket,threading,time
from com.zxy.tcp.TcpServer import TcpServer
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--TCP/IP Server线程
class ServerThread(z_debug):
attDtTimeOut = 60
attInput = ""
attNum = ""
attPort = 0
attThreadSize = 500
def __init__(self, temInput, temNum, temPort):
self.attInput = temInput
self.attNum = temNum
self.attPort = temPort
def run(self):
global dServThreadList
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', self.attPort))
s.listen(self.attThreadSize)
while True:
try:
sock, addr = s.accept()
ckeys = sock.getpeername()[0] + '|' + str(sock.getpeername()[1])
ts = TcpServer(ckeys)
t = threading.Thread(target=ts.server_link, args=(sock, addr), name="ServerClient" + ckeys)
t.start()
except Exception as en:
if str(type(self)) == "<class 'type'>":
self.debug_in(self,repr(en)+"=>"+str(en.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(en)+"=>"+str(en.__traceback__.tb_lineno))#打印异常信息
finally:
time.sleep(0.1)
except Exception as e:
if str(type(self)) == "<class 'type'>":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
Pass
新建作为TCP Server接收数据拦截器插件类com.plugins.usereflect.testServerReflectInClass1.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--作为TCP Server接收数据拦截器插件,可做设备反控操作程序
class testServerReflectInClass1(z_debug):
#接收到数据
strResult = ""
#需要发送到客户端数据
strSend = ""
#是否继续执行 1:继续执行 0:中断执行
strContinue = "1"
#所连接的远端IP
strIP = ""
#端口号
strPort = ""
def __init__(self, params):
pass
def init_start(self):
#作为服务端接收数据拦截器
if self.strResult == "001":
self.strSend = "#send to client data:client"
print("AAAA作为服务端接收数据拦截器:"+self.strResult)
self.strResult = "ok"
新建作为TCP Server接收数据拦截器插件类com.plugins.usereflect.testServerReflectInClass2.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--作为TCP Server接收数据拦截器插件,可做设备反控操作程序
class testServerReflectInClass2(z_debug):
#接收到数据
strResult = ""
#需要发送到客户端数据
strSend = ""
#是否继续执行 1:继续执行 0:中断执行
strContinue = "1"
#所连接的远端IP
strIP = ""
#端口号
strPort = ""
def __init__(self, params):
pass
def init_start(self):
#作为服务端接收数据拦截器
if self.strResult == "002":
self.strSend = "#send to client data:client"
print("BBB作为服务端接收数据拦截器:"+self.strResult)
self.strResult = "ok"
在com.zxy.main.Init_Page.py中添加代码
from com.zxy.tcp.ServerThread import ServerThread
@staticmethod
def Start_Server():
st = ServerThread("", "", int(Com_Para.AccSocketPort))
# TCP服务端案例
t2 = threading.Thread(target=st.run, name="ServerMainThread")
t2.start()
TCP Server测试案例MonitorDataCmd.py主文件中编写:
在 if __name__ == '__main__':下添加
#TCP Server接收数据拦截器插件
Com_Para.ServerREFLECT_IN_CLASS = "com.plugins.usereflect.testServerReflectInClass1"
#TCP Server配置端口参数
Com_Para.AccSocketPort = "8099"
#TCP/IP Server初始化
Init_Page.Start_Server()
#暂停120秒,等待client连接
time.sleep(120)
#遍历已经连接到本设备的tcp client并发送数据包
for key in Com_Para.dServThreadList.keys():
temSck = Com_Fun.GetHashTableNone(Com_Para.dServThreadList, key)
temValue = "Server往Client端发送数据包:"+Com_Fun.GetTimeDef()
Com_Fun.SendSocket(temValue, temSck)
print("=>"+key+"=>"+temValue)
运行测试结果如下图:
文章来源:https://www.toymoban.com/news/detail-684937.html
监测数据采集物联网应用开发步骤(10)文章来源地址https://www.toymoban.com/news/detail-684937.html
到了这里,关于13、监测数据采集物联网应用开发步骤(9.2)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!