Cheepsss

server

Dec 2nd, 2021 (edited)
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket
  2. import json
  3. import time
  4.  
  5. HOST = "127.0.0.1"
  6. PORT = 8085
  7. BUFFER_SIZE = 1024
  8. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  9. #s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1)
  10. s.bind((HOST, PORT))
  11. addresses = []
  12.  
  13. while True:
  14.     message, address = s.recvfrom(BUFFER_SIZE)
  15.     message = json.loads(message)
  16.     if message[1] == "join_server":
  17.         if address not in addresses:
  18.             addresses.append(address)
  19.             print(message[0] + " joined the server")
  20.     else:
  21.         for addr in addresses:
  22.             s.sendto(bytes((json.dumps(message)), "utf-8"), addr)
  23.             print(message)
  24.         if message[1] == "quit_server":
  25.             print(message[0] + " left the server")
  26.             s.sendto(bytes((json.dumps((message[0],"left the server"))), "utf-8"), addr)
  27.             addresses.remove(address)
RAW Paste Data