Facebook
From eeeeee, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 81
  1. import socket
  2. import threading
  3.  
  4. HOST = "127.0.0.1"
  5. PORT = 5000
  6.  
  7. connected = True
  8.  
  9.  
  10. class SendThread(threading.Thread):
  11.     def __init__(self, class_socket):
  12.         threading.Thread.__init__(self)
  13.         self.s_socket = class_socket
  14.  
  15.     def run(self):
  16.         while True:
  17.             message = input()
  18.             if message.lower().strip() == 'quit':
  19.                 self.s_socket.send("quit".encode())
  20.                 break
  21.             self.s_socket.send(message.encode())
  22.  
  23.  
  24. def client_program():
  25.     host = HOST
  26.     port = PORT
  27.  
  28.     client_socket = socket.socket()
  29.     client_socket.connect((host, port))
  30.  
  31.     send_thread = SendThread(client_socket)
  32.     send_thread.start()
  33.  
  34.     while True:
  35.         data = client_socket.recv(1024).decode()
  36.         if data == "quit":
  37.             break
  38.         print(data)
  39.  
  40.     client_socket.close()
  41.  
  42.  
  43. if __name__ == '__main__':
  44.     client_program()
  45.