200行python实现自定义TCP客户端 带界面和断线重连

需要一个带界面带按钮和回显且自定义字符的网络调试助手.

用python是图方便修改,不用编译建立工程文本编辑器改完就能运行.

用Tkinter是因为python自带不臃肿,

.实际只是记录一下踩坑过程. 花了一个小时写了二百五十行.

r"""    

N=1/2/3/4/5/6
手动操作:= A(右侧)  B(左侧) 

B=左侧

TCP_server_L
左:7931
1/2/3/4/5/6
CamB_N

#-----------------------
A=右侧
TCP_server_R
右:7930
CamA_N


"""

r"""
CamB_1 
CamA_1 
CamB_2 
CamA_2 
CamB_3 
CamA_3 
CamB_4 
CamA_4 
CamB_5 
CamA_5 
CamB_6 
CamA_6 
"""
r"""
TCP server:
	IP:192.168.1.41
	Port: 左:7931  右:7930

S1>B
S2>A

产品:
| 编号  |尺寸      |高度|左触发字符|右触发字符|
| 1.    |38x25x8   |8   |  CamB_1  |  CamA_1  | ;
| 2.    |44x21x15.8|15.8|  CamB_2  |  CamA_2  | ;
| 3.    |57x26x20  |20  |  CamB_3  |  CamA_3  | ;
| 4.    |108x19x15 |15  |  CamB_4  |  CamA_4  | ;
| 5.    |25x13     |13  |  CamB_5  |  CamA_5  | ;
| 6.    |17x10     |10  |  CamB_6  |  CamA_6  | ;

左侧触发字符:
	CamB_1
	返回:cam1_1_1_x1_y1_a1_x2_y2_a2_x3_y3_a3
	缺料:cam1_2
右侧触发字符:
	CamA_1
	返回:cam2_1_1_x1_y1_a1_x2_y2_a2_x3_y3_a3
	缺料:cam2_2
		
在当前方案  发送当前方案触发字符 可执行当前方案流程 即拍照
不在当前方案 发送触发字符   切换方案  不会返回字符
1/2/3/4/5/6 切换方案
("192.168.1.41", 7931)#左侧
比如:("192.168.1.41", 7931) 发1 则切换料号1
比如:("192.168.1.41", 7931) 发6 则切换料号6

机器人IP:
    A机器人:
    192.168.1.30>>(server:7930)"CamA_3">>右侧
    B 机器人:
    192.168.1.31>>(server:7931)"CamB_3">>左侧
"""

def p(P):print(P);
import os,sys,time,socket,_thread,threading
def thread_it(func, *args):
    t = threading.Thread(target=func, args=args)
    t.setDaemon(True);
    t.start();
def Thread_ConnectSocket(ip="",prot=0,sendData="",recv_flag=True):
    是否发送成功标志=False
    while True:
        try:
            global tcp_client_socket
            tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            tcp_client_socket.connect((ip, prot))
            while True:
                try:
                    tcp_client_socket.send(sendData.encode("utf-8"))#发送
                    print(f'>>{ip}:({prot})"{sendData}"!')
                    if (recv_flag):
                        #print(f'等待对方回复!')
                        recv_content = tcp_client_socket.recv(1024)#接收
                        result = recv_content.decode("utf-8")
                        print(f'收到对方发来的"{result}"!')   
                    print(f'done!')
                    是否发送成功标志=True
                    break
                except:
                    是否发送成功标志=False
                    break
        except socket.error:
            print("未连接,尝试重连中..")
            time .sleep(1)
        if (是否发送成功标志):
            print(f'finish!')
            break


ipportB=("192.168.1.41", 7931)#左侧
ipportA=("192.168.1.41", 7930)#右侧


IPA=ipportA[0]
PROTA=ipportA[1] #IP,PROT 
IPB=ipportB[0]
PROTB=ipportB[1] #IP,PROT 
#B_左(7931)  A_右(7930) 
def Tcp_SendB(S=""):#B_左(7931)
    thread_it(Thread_ConnectSocket,IPB,PROTB,S)
def Tcp_SendA(S=""):#A_右(7930) 
    thread_it(Thread_ConnectSocket,IPA,PROTA,S)
#B_左(7931)  A_右(7930) 

#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 

import tkinter as tk

root = tk.Tk()#root.iconbitmap('mfc.ico')
root.geometry('400x850+100+100')
root.resizable(0,0)
root.title("")
lb__a = tk.Label(root,text=f"B_左(7931) :{IPB} : {PROTB}",fg='green',font=("微软雅黑",16))
lb__a.pack()
lb__a = tk.Label(root,text=f"A_右(7930) :{IPA} : {PROTA}",fg='green',font=("微软雅黑",16))
lb__a.pack()
def gettime():
    dstr.set(time.strftime("%H:%M:%S"))
    root.after(1000, gettime)# 每隔 1s 调用一次
dstr = tk.StringVar()# 定义动态字符串
lb = tk.Label(root,textvariable=dstr,fg='green',font=("微软雅黑",20))
lb.pack()
gettime()


#------------------------------料号1---------------------------------------------------------
#---------------------------------------------------------------------------------------------
def CamB_1():
    #thread_it(Thread_ConnectSocket,IP,PROT,"CamB_1")
    Tcp_SendB("CamB_1")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='1.左|38x25x8|CamB_1',bg='#7CCD7C',width=20, height=2,command=CamB_1)
button.pack(side="top",anchor="nw")


#------------------------------料号1---------------------------------------------------------
#---------------------------------------------------------------------------------------------

def CamA_1():
    Tcp_SendA("CamA_1")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='1.右|38x25x8|CamA_1',bg='#7CCD7C',width=20, height=2,command=CamA_1)
button.pack(side="top",anchor="nw")
#---------------------------------------------------------------------------------------------
#------------------------------料号2---------------------------------------------------------
#---------------------------------------------------------------------------------------------
def CamB_2():
    Tcp_SendB("CamB_2")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='2.左|44x21x15.8|CamB_2',bg='#7CCD7C',width=20, height=2,command=CamB_2)
button.pack(side="top",anchor="nw")

def CamA_2():
    Tcp_SendA("CamA_2")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='2.右|44x21x15.8|CamA_2',bg='#7CCD7C',width=20, height=2,command=CamA_2)
button.pack(side="top",anchor="nw")
#---------------------------------------------------------------------------------------------
#------------------------------料号3---------------------------------------------------------
#---------------------------------------------------------------------------------------------
def CamB_3():
    Tcp_SendB("CamB_3")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='3.左|57x26x20|CamB_3',bg='#7CCD7C',width=20, height=2,command=CamB_3)
button.pack(side="top",anchor="nw")

def CamA_3():
    Tcp_SendA("CamA_3")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='3.右|57x26x20|CamA_3',bg='#7CCD7C',width=20, height=2,command=CamA_3)
button.pack(side="top",anchor="nw")
#---------------------------------------------------------------------------------------------
#------------------------------料号4---------------------------------------------------------
#---------------------------------------------------------------------------------------------
def CamB_4():
    Tcp_SendB("CamB_4")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='4.左|108x19x15|CamB_4',bg='#7CCD7C',width=20, height=2,command=CamB_4)
button.pack(side="top",anchor="nw")

def CamA_4():
    Tcp_SendA("CamA_4")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='4.右|108x19x15|CamA_4',bg='#7CCD7C',width=20, height=2,command=CamA_4)
button.pack(side="top",anchor="nw")
#---------------------------------------------------------------------------------------------
#------------------------------料号5---------------------------------------------------------
#---------------------------------------------------------------------------------------------
def CamB_5():
    Tcp_SendB("CamB_5")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='5.左|25x13|CamB_5',bg='#7CCD7C',width=20, height=2,command=CamB_5)
button.pack(side="top",anchor="nw")

def CamA_5():
    Tcp_SendA("CamA_5")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='5.右|25x13|CamA_5',bg='#7CCD7C',width=20, height=2,command=CamA_5)
button.pack(side="top",anchor="nw")
#---------------------------------------------------------------------------------------------
#------------------------------料号6---------------------------------------------------------
#---------------------------------------------------------------------------------------------
def CamB_6():
    Tcp_SendB("CamB_6")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='6.左|17x10|CamB_6',bg='#7CCD7C',width=20, height=2,command=CamB_6)
button.pack(side="top",anchor="nw")

def CamA_6():
    Tcp_SendA("CamA_6")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='6.右|17x10|CamA_6',bg='#7CCD7C',width=20, height=2,command=CamA_6)
button.pack(side="top",anchor="nw")
#---------------------------------------------------------------------------------------------

#B_左(7931) 
def Qhlh_1():
    Tcp_SendB("1")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='1',bg='#7CCD7C',width=20, height=2,command=Qhlh_1)
button.place(x=200-20,y=110, width=100, height=80)
def Qhlh_2():
    Tcp_SendB("2")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='2',bg='#7CCD7C',width=20, height=2,command=Qhlh_2)
button.place(x=200-20,y=110*2+5, width=100, height=80)
def Qhlh_3():
    Tcp_SendB("3")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='3',bg='#7CCD7C',width=20, height=2,command=Qhlh_3)
button.place(x=200-20,y=110*3+10, width=100, height=80)
def Qhlh_4():
    Tcp_SendB("4")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='4',bg='#7CCD7C',width=20, height=2,command=Qhlh_4)
button.place(x=200-20,y=110*4+15, width=100, height=80)
def Qhlh_5():
    Tcp_SendB("5")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='5',bg='#7CCD7C',width=20, height=2,command=Qhlh_5)
button.place(x=200-20,y=110*5+20, width=100, height=80)
def Qhlh_6():
    Tcp_SendB("6")#Tcp_SendB Tcp_SendA #B_左(7931)  A_右(7930) 
button = tk.Button(root,text='6',bg='#7CCD7C',width=20, height=2,command=Qhlh_6)
button.place(x=200-20,y=110*6+25, width=100, height=80)
#---------------------------------------------------------------------------------------------
root.mainloop()

只能说用着比一般网络助手方便些.

一直以为串口有自定义字符的网络助手,网口没有.

python做完了才发现这个;

看来还是这个省事啊,早知道这个也能用那不是白折腾了.python

不过python可以断线重连. 塞到线程虽然不规范, 但已经满足测试需要.脚本就是要代替人工重复劳动不是么.

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值