Guest User

server.py

a guest
Dec 5th, 2021
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket, threading
  2. from tkinter import *
  3.  
  4. HOST = socket.gethostbyname(socket.gethostname())
  5. PORT = 12345
  6. FORMAT = "utf-8"
  7. BUFSIZ = 1024
  8.  
  9. class Server():
  10.     def __init__(self, host, port):
  11.         self.host = host
  12.         self.port = port
  13.         self.clients, self.nicknames = [], []
  14.         self.s = None
  15.  
  16.         self.window = Tk()
  17.         self.window.title("Server")
  18.         self.window.config(bg="#434646")
  19.         self.window.resizable(height=False, width=False)
  20.  
  21.         self.topFrame = Frame(self.window, bg="#434646")
  22.  
  23.         self.btnStart = Button(
  24.                 self.topFrame,
  25.                 text="Connect",
  26.                 font=("Century", 12, "bold"),
  27.                 height=1,
  28.                 width=15,
  29.                 bd=3,
  30.                 bg="lightgray",
  31.                 fg="#434646",
  32.                 command=lambda: self.start_server()
  33.             )
  34.         self.btnStart.pack(side=LEFT, padx=(25, 0))
  35.  
  36.         self.btnStop = Button(
  37.                 self.topFrame,
  38.                 text="Stop",
  39.                 font=("Century", 12, "bold"),
  40.                 height=1,
  41.                 width=15,
  42.                 bd=3,
  43.                 bg="lightgray",
  44.                 fg="#434646",
  45.                 command=lambda: self.stop_server(),
  46.                 state=DISABLED
  47.             )
  48.         self.btnStop.pack(side=LEFT, padx=(25, 25))
  49.  
  50.         self.topFrame.pack(side=TOP, pady=(5, 0))
  51.  
  52.  
  53.         self.midFrame = Frame(self.window, bg="#434646")
  54.         self.lblCave = Label(
  55.                 self.midFrame,
  56.                 text="Cave name: <Connect to the server first>",
  57.                 bg="#434646",
  58.                 fg="lightgray",
  59.                 font=("Helvetica", 12, "bold")
  60.             )
  61.         self.lblCave.pack(side=LEFT)
  62.  
  63.         self.midFrame.pack(side=TOP, pady=(5, 0))
  64.  
  65.  
  66.         self.clientFrame = Frame(self.window, bg="#434646")
  67.         self.line_lbl = Label(
  68.                 self.clientFrame,
  69.                 text="Connected Clients",
  70.                 bg="#434646",
  71.                 fg="lightgray",
  72.                 font=("Century", 12, 'bold')
  73.             ).pack()
  74.         self.scrollbar = Scrollbar(self.clientFrame)
  75.         self.scrollbar.pack(side=RIGHT, fill=Y)
  76.         self.tkDisplay = Text(self.clientFrame, height=15, width=50, bg="lightgray")
  77.         self.tkDisplay.pack(side=LEFT, fill=Y, padx=(5, 0))
  78.         self.scrollbar.config(command=self.tkDisplay.yview)
  79.         self.tkDisplay.config(yscrollcommand=self.scrollbar.set, state=DISABLED)
  80.  
  81.         self.clientFrame.pack(side=BOTTOM, pady=(5, 0))
  82.  
  83.         self.window.mainloop()
  84.  
  85.  
  86.     def start_server(self):
  87.         self.btnStart.config(state=DISABLED)
  88.         self.btnStop.config(state=NORMAL)
  89.  
  90.         self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  91.         self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  92.  
  93.         self.s.bind((self.host, self.port))
  94.         self.s.listen(5)
  95.  
  96.         threading.Thread(target=self.accept_incoming_connections, args=(self.s,)).start()
  97.  
  98.         self.lblCave["text"] = f"Cave name: {socket.gethostname()}"
  99.         print("Cave name: %s" %socket.gethostname())
  100.  
  101.  
  102.     def stop_server(self):
  103.         self.s.shutdown(socket.SHUT_RDWR)
  104.         self.s.close()
  105.  
  106.         self.lblCave["text"] = "Cave name: <Connect to the server first>"
  107.  
  108.         self.btnStart.config(state=NORMAL)
  109.         self.btnStop.config(state=DISABLED)
  110.  
  111.  
  112.     def broadcastMessage(self, message):
  113.         for client in self.clients:
  114.             client.send(bytes(message, FORMAT))
  115.  
  116.  
  117.     def handle_client(self, client):
  118.         while True:
  119.             try:
  120.                 message = client.recv(BUFSIZ)
  121.                 broadcastMessage(message)
  122.  
  123.             except:
  124.                 index = self.clients.index(client)
  125.                 self.clients.remove(client)
  126.                 client.close()
  127.                 nickname = self.nicknames[index]
  128.                 self.nicknames.remove(nickname)
  129.                 break
  130.  
  131.  
  132.     def accept_incoming_connections(self, server):
  133.         while True:
  134.             client, address = server.accept()
  135.  
  136.             client.send(bytes("NICK", FORMAT))
  137.             nickname = client.recv(BUFSIZ).decode(FORMAT)
  138.  
  139.             self.nicknames.append(nickname)
  140.             self.clients.append(client)
  141.  
  142.             print(f"{str(address)} entered the cave! Nickname is {nickname}")
  143.             self.broadcastMessage(f"{nickname} entered the cave!\n")
  144.             client.send(bytes("Connected with the server", FORMAT))
  145.  
  146.             threading.Thread(target=self.handle_client, args=(client,)).start()
  147.  
  148.  
  149. s = Server(HOST, PORT)
RAW Paste Data