n0va_sa

Multithreaded Chat Server

Sep 20th, 2017
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Chat Server#
  2. import socket, threading
  3.  
  4. def incoming(client):
  5.     while(True):
  6.         data = client.recv(1024).decode('utf-8')
  7.         print('\nClient:$ '+ data)
  8.  
  9. def main():
  10.     thread_list = []
  11.     port = int(input('Enter Port: '))
  12.     server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.     server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  14.     server.bind(('0.0.0.0',port))
  15.     print("Waiting For Connection: ")
  16.     server.listen(1)
  17.     client, addr = server.accept()
  18.     print("Connected to " + str(addr))
  19.  
  20.     incomingThread = threading.Thread(target=incoming, args=(client,))
  21.     thread_list.append(incomingThread)
  22.     incomingThread.start()
  23.     while(1):
  24.         data = input("\n:$ ")
  25.         client.send(data.encode('utf-8'))
  26.  
  27. if __name__ == "__main__":
  28.     main()
RAW Paste Data