n0va_sa

Multithreaded Chat Client

Sep 20th, 2017
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Chat Client #
  2. import socket, threading
  3.  
  4. def incoming(server):
  5.     while True:
  6.         data = server.recv(2000).decode('utf-8')
  7.         print("Server: " + data)
  8. def main():
  9.     host = input("Server Host: ")
  10.     port = int(input("Server Port: "))
  11.     server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12.  
  13.     server.connect((host,port))
  14.     thread_list = []
  15.     data = 'abc'
  16.     incomingThread = threading.Thread(target=incoming,args=(server,))
  17.     thread_list.append(incomingThread)
  18.     incomingThread.start()
  19.  
  20.     while data != 'exit!':
  21.         data = input("\n$: ")
  22.         server.send(data.encode('utf-8'))
  23. main()
RAW Paste Data