您当前的位置: 首页 >  Python

程序员正茂

暂无认证

  • 2浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python TCP客户端自动重连

程序员正茂 发布时间:2020-10-22 17:35:14 ,浏览量:2

import os,sys,time
import socket

def main():
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.settimeout(5)    

    connect = False
    while True:        
        try:
            if connect == False:
                client.connect(("192.168.0.101",60000))
                connect = True
            client.send(bytes('hello', 'UTF-8'))
        except socket.error as  e:
            print("Address-related error connecting to server: %s" % e)
            connect = False
        time.sleep(1)

if __name__ == "__main__" :
    main()

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0352s