SHARE
TWEET

ComThreads

a guest Mar 24th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket
  2. import sys
  3. from threading import Thread
  4.  
  5. def recv_from_client(conn, addr):
  6.     while True:
  7.         try:
  8.             #Pega o que o cliente enviou
  9.             data = conn.recv(1024)
  10.             #Mostra o que o cliente enviou
  11.             print("'{}:{}' received from {}".format(*addr, data.decode("utf-8")))
  12.         finally:
  13.             conn.close()
  14.             break
  15.  
  16. if __name__ == "__main__":
  17.     # Create a TCP/IP socket
  18.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19.  
  20.     # Bind the socket to the port
  21.     server_address = ('localhost', 10000)
  22.     print('starting up on {} port {}'.format(*server_address))
  23.     sock.bind(server_address)
  24.  
  25.     # Listen for incoming connections
  26.     sock.listen(5)
  27.     while True:
  28.         # Wait for a connection
  29.         print('waiting for a connection')
  30.         connection, client_address = sock.accept()
  31.  
  32.         #Cria um thread para cada cliente connectado
  33.         Thread(target=recv_from_client, args=(connection, client_address,)).start()
  34.     sock.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top