Advertisement
vanjavk

domicatest

Oct 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. #TCp client
  2. import socket
  3.  
  4. global ip,port,s
  5. ip = '93.141.164.3'
  6. port = 13337
  7. BUFFER_SIZE = 1024
  8. MESSAGE = "Hello, World! domicatest"
  9. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10. s.settimeout(5)
  11. #s.connect((TCP_IP, TCP_PORT))
  12.  
  13.  
  14.  
  15. def connect():
  16.     try:
  17.         s.connect((ip, port))
  18.     except Exception as e:
  19.         s.close()
  20.         print("connect exception",e)
  21.         return False
  22.     return True
  23.  
  24. def keepconnecting():
  25.     while connect()==False:
  26.         print("Connection error")
  27.    
  28. keepconnecting()
  29.  
  30. while 1:
  31.     try:
  32.        
  33.         s.send(MESSAGE.encode())
  34.         data = s.recv(BUFFER_SIZE)
  35.        
  36.         print ("received data:", data.decode())
  37.     except Exception as e:
  38.         print("Connection error!")
  39.         print(e)
  40.         if "[WinError 10054]" in str(e) or str(e)=="":
  41.             s.close()
  42.             print("I'll try to reconnect")
  43.             keepconnecting()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement