您当前的位置: 首页 >  Python

程序员正茂

暂无认证

  • 2浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python异步TCP自动重连客户端

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


import os,sys,time
import socket
import errno
from time import sleep
 
def main():
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.setblocking(0)
    connect = False
    while True:        
        try:
            if connect == False:
                client.connect(("192.168.0.101",60000))
                connect = True
            client.send(bytes('hello', 'UTF-8'))
            msg = client.recv(4096)
        except socket.error as  e:
            err = e.args[0]
            if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                print('No data available')
            elif err == errno.EINPROGRESS:
                print('Connecting')
            else:
                # a "real" error occurred
                print(e
)
                connect = False
                client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                client.setblocking(0)
        else:
            # got a message, do something :)
            print("Receive '%s'" % msg)
        time.sleep(0.2)
 
if __name__ == "__main__" :
    main()

 

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

微信扫码登录

0.0414s