13、监测数据采集物联网应用开发步骤(9.2)

  1. 监测数据采集物联网应用开发步骤(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)

运行测试结果如下图:

监测数据采集物联网应用开发步骤(10)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值