当前位置:   article > 正文

Python异步TCP自动重连客户端_asyncio.protocol socket 自动重连

asyncio.protocol socket 自动重连
  1. import os,sys,time
  2. import socket
  3. import errno
  4. from time import sleep
  5. def main():
  6. client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  7. client.setblocking(0)
  8. connect = False
  9. while True:
  10. try:
  11. if connect == False:
  12. client.connect(("192.168.0.101",60000))
  13. connect = True
  14. client.send(bytes('hello', 'UTF-8'))
  15. msg = client.recv(4096)
  16. except socket.error as e:
  17. err = e.args[0]
  18. if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
  19. print('No data available')
  20. elif err == errno.EINPROGRESS:
  21. print('Connecting')
  22. else:
  23. # a "real" error occurred
  24. print(e
  25. )
  26. connect = False
  27. client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  28. client.setblocking(0)
  29. else:
  30. # got a message, do something :)
  31. print("Receive '%s'" % msg)
  32. time.sleep(0.2)
  33. if __name__ == "__main__" :
  34. main()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/91439
推荐阅读
相关标签
  

闽ICP备14008679号