Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import socket
  2. import threading
  3.  
  4. class ThreadedServer(object):
  5.     def __init__(self, host, port):
  6.         self.host = host
  7.         self.port = port
  8.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9.         self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  10.         self.sock.bind((self.host, self.port))
  11.         self.clients = {}
  12.  
  13.     def listen(self):
  14.         self.sock.listen(5)
  15.         while True:
  16.             client, address = self.sock.accept()
  17.             client.settimeout(60)
  18.             self.clients[client] = ''
  19.             threading.Thread(target = self.listenToClient,args = (client,address)).start()
  20.  
  21.     def listenToClient(self, client, address):
  22.         size = 1024
  23.         while self.clients[client] == '':
  24.             client.send(b"Please pick a username: ")
  25.             username = client.recv(size)
  26.             if username in self.clients.values():
  27.                 client.send(b"User iname is in use. ")
  28.             else:
  29.                 self.clients[client] = username
  30.  
  31.         other_users = [x.decode() for x in self.clients.values()]
  32.         welcome_string = "Welcome, other users connected: {}".format(",".join(other_users))
  33.         client.send(welcome_string.encode())
  34.  
  35.         while True:
  36.             try:
  37.                 data = client.recv(size)
  38.                 if data:
  39.                     # Set the response to echo back the recieved data
  40.                     response = data
  41.                     self.send_to_others(client)
  42.                 else:
  43.                     raise error('Client disconnected')
  44.                     self.send_to_others(client, '{} disconnected'.format(username).encode())
  45.                     self.client.remove(client)
  46.             except:
  47.                 client.close()
  48.                     self.send_to_others(client, '{} disconnected'.format(username).encode())
  49.                 del self.clients[client]
  50.                 return False
  51.  
  52.     def send_to_others(self, me, message):
  53.         for other_client in self.clients:
  54.             if other_client is not me:
  55.                 response_string = "{}: {}".format(self.clients[me], message.decode())
  56.                 other_client.send(response_string.encode())
  57.  
  58. if __name__ == "__main__":
  59.     while True:
  60.         port_num = 44445 # input("Port? ")
  61.         try:
  62.             port_num = int(port_num)
  63.             break
  64.         except ValueError:
  65.             pass
  66.  
  67.     ThreadedServer('',port_num).listen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement