Advertisement
mgostih

Client

Feb 3rd, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. from threading import Thread
  2. from time import sleep
  3. from urllib.request import urlopen
  4. HOST = urlopen("Sitocontenente l'host").read().decode("utf-8")    # The remote host
  5. PORT = 50007            # The same port as used by the server
  6.  
  7. def trasmissione():
  8.     global s, HOST
  9.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10.     try:
  11.         s.connect((HOST, PORT))
  12.     except:
  13.         print("Connessione interrotta. Riconnessione tra 10 secondi.")
  14.         HOST = urlopen("Sitocontenente l'host").read().decode("utf-8") #In case the server IP change address/host
  15.         sleep(10)
  16.         trasmissione()
  17.     t1 = Thread(target=ricezione).start()
  18.     while True:
  19.         sleep(0.3)
  20.         a = input(">>> ")
  21.         a = a.encode("cp1252")
  22.         try:
  23.             s.sendall(a)
  24.         except:
  25.             print("Connessione interrotta. Riconnessione tra 10 secondi.")
  26.             sleep(10)
  27.             trasmissione()
  28. def ricezione():
  29.     while True:
  30.         try:
  31.             data = s.recv(1024)
  32.         except:
  33.             break
  34.         print(data.decode("cp1252"))
  35. trasmissione()
  36.  
  37.  
  38. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement