python--socket心跳+接收轮询+自动重连+多线程
1、下位机给上位机间隔发送心跳
2、下位机收到上位机的指令时,返回相应参数(这里写固定死了)
3、直接放while循环里,实现断线重连
4、使用了17个线程模拟17个下位机
# -*- coding:utf-8 -*- # Author : tang # Data : 2020-06-05 9:02:56 import socket import time,threading def tt1(tcp_port,tcp_heartbeat,receive): #一个主线程模拟一个下位机,tcp_port端口,tcp_heartbeat心跳包,receive数据 def doConnect(ip_port): #建立socket连接 sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sk.connect(ip_port) except: pass return sk def heartbeats(sk): #心跳 while True: msg = str(time.strftime("%Y-%m-%d %H:%M:%S")) try: heartbeat = tcp_heartbeat sk.sendall(bytes(heartbeat, 'utf8')) except socket.error: print(f'\r\nsocket error,do reconnect:{msg}') time.sleep(3) break except: print(f'\r\nother error occur:{msg}') time.sleep(3) break time.sleep(30) def rec_send(sk): #接收数据并返回 while True: msg = str(time.strftime("%Y-%m-%d %H:%M:%S")) try: cmd=receive sk.recv(1024) sk.sendall(cmd) except socket.error: print(f'\r\nsocket error,do reconnect:{msg}\n{receive}') time.sleep(3) break except: print(f'\r\nother error occur:{msg}\n{receive}') time.sleep(3) break ip_port = ('139.196.194.247', tcp_port) while True: sk = doConnect(ip_port) # print('客户端启动:') t1=threading.Thread(target=heartbeats,args=(sk,)) #心跳线程 t2=threading.Thread(target=rec_send,args=(sk,)) #收发线程 t1.start() t2.start() t1.join() t2.join() return1=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x31' return2=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x32' return3=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x33' return4=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x34' return5=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x35' return6=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x36' return7=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x37' return8=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x38' return9=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x39' return10=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x40' return11=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x41' return12=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x42' return13=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x43' return14=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x44' return15=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x45' return16=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x46' return17=b'\xFB\x69\x08\x50\x22\x02\x07\x18\x10\x07\x47' client1=threading.Thread(target=tt1,args=(40021,'sczy21',return1,)) client2=threading.Thread(target=tt1,args=(40022,'sczy22',return2,)) client3=threading.Thread(target=tt1,args=(40023,'sczy23',return3,)) client4=threading.Thread(target=tt1,args=(40024,'sczy24',return4,)) client5=threading.Thread(target=tt1,args=(40025,'sczy25',return5,)) client6=threading.Thread(target=tt1,args=(40026,'sczy26',return6,)) client7=threading.Thread(target=tt1,args=(40027,'sczy27',return7,)) client8=threading.Thread(target=tt1,args=(40028,'sczy28',return8,)) client9=threading.Thread(target=tt1,args=(40029,'sczy29',return9,)) client10=threading.Thread(target=tt1,args=(40030,'sczy30',return10,)) client11=threading.Thread(target=tt1,args=(40031,'sczy31',return11,)) client12=threading.Thread(target=tt1,args=(40032,'sczy32',return12,)) client13=threading.Thread(target=tt1,args=(40033,'sczy33',return13,)) client14=threading.Thread(target=tt1,args=(40034,'sczy34',return14,)) client15=threading.Thread(target=tt1,args=(40035,'sczy35',return15,)) client16=threading.Thread(target=tt1,args=(40036,'sczy36',return16,)) client17=threading.Thread(target=tt1,args=(40037,'sczy37',return17,)) client1.start() time.sleep(0.5) client2.start() time.sleep(0.5) client3.start() time.sleep(0.5) client4.start() time.sleep(0.5) client5.start() time.sleep(0.5) client6.start() time.sleep(0.5) client7.start() time.sleep(0.5) client8.start() time.sleep(0.5) client9.start() time.sleep(0.5) client10.start() time.sleep(0.5) client11.start() time.sleep(0.5) client12.start() time.sleep(0.5) client13.start() time.sleep(0.5) client14.start() time.sleep(0.5) client15.start() time.sleep(0.5) client16.start() time.sleep(0.5) client17.start() time.sleep(0.5) client1.join() client2.join() client3.join() client4.join() client5.join() client6.join() client7.join() client8.join() client9.join() client10.join() client11.join() client12.join() client13.join() client14.join() client15.join() client16.join() client17.join()
追赶曾经蹉跎的岁月