Advertisement
Abdullahafzalkhan

Untitled

Mar 20th, 2023
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. from customtkinter import *
  2. import socket
  3. import threading
  4. import json
  5. import atexit
  6. from os import getcwd
  7. from time import sleep
  8.  
  9. HEADER = 1024
  10. PORT = 5050
  11. SERVER = socket.gethostbyname(socket.gethostname()) #"192.168.1.11"
  12. ADDR = (SERVER, PORT)
  13. DISCONNET_MESSAGE = "!DISCONNET"
  14. CWD = getcwd()
  15.  
  16. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17. server.bind(ADDR)
  18. attempts = {}
  19.  
  20. file = open(f"{CWD}/players.json","r")
  21. json_data = json.load(file)
  22. ips = json_data["Ips"]
  23. file.close()
  24.  
  25. player_data = json_data["Players"]
  26.  
  27. def handle_client(conn, addr):
  28.     global player_data, ips
  29.     print(f"[NEW CONNETION] {addr} connected")
  30.     loggedin = False
  31.     ip = addr[0]
  32.     print(ip, "--------------------------------------------------------------------")
  33.  
  34.     connected = True
  35.     if ips[ip]["attempts"] < 1:
  36.         print("Max", addr[0])
  37.         sleep(5)
  38.         maxAttempts = False
  39.         ips[ip]["attempts"] = 3
  40.         conn.send("LoginMessage:-Tryagain".encode("utf-8"))
  41.     while connected:
  42.         msg = conn.recv(HEADER).decode('utf-8')
  43.         ##################################################################################
  44.         if "Login:-" in msg: # handling login
  45.             if not ips[ip]["attempts"] < 1:
  46.                 username, password = msg.split("Login:-")
  47.                 if username in player_data: # checking if the username is correct
  48.                     if password == player_data[username]["password"]:
  49.                         player = player_data[username]
  50.                         data = f"{player['username']}:-{player['balance']}:-Success:-LoginMessage".encode("utf-8")
  51.                         player_data[username]["Online"] = True
  52.                         conn.send(data)
  53.                         loggedin = True
  54.                         ips[ip]["attempts"] = 3
  55.                         break
  56.  
  57.                 if not loggedin:
  58.                     if ips[ip]["attempts"] < 1:
  59.                         conn.send("Maxattempts:-LoginMessage".encode("utf-8"))
  60.                         print("Maxattempts")
  61.                         ips[ip]["attempts"] = 0
  62.                         print("attempts:",ips[ip]["attempts"])
  63.                     else:
  64.                         ips[ip]["attempts"] = ips[ip]["attempts"] - 1
  65.                         print("attempts:",ips[ip]["attempts"])
  66.                         conn.send(f"Denied:-{ips[ip]['attempts']}:-LoginMessage".encode("utf-8"))
  67.                        
  68.             else:
  69.                 conn.send("Maxattempts".encode("utf-8"))
  70.                 maxAttempts = True
  71.         ##################################################################################
  72.         if msg == DISCONNET_MESSAGE:
  73.             connected = False
  74.             print(f"[CONNETION] {addr} disconnected")
  75.     change(ips, player_data)
  76.     conn.close()
  77.  
  78. def change(ip, player):
  79.     ips = ip
  80.     player_data = player
  81.  
  82. def Exit():
  83.     file = open(f"{CWD}/players.json", "w+")
  84.     data = {
  85.             'Players': player_data,
  86.             'Ips': ips
  87.         }
  88.     print(data)
  89.     json.dump(data, file,indent=6)
  90.     file.close()
  91.  
  92. def start():
  93.     global player_data, ips
  94.     server.listen()
  95.     print(f"SERVER IS listening on {SERVER}")
  96.     while True:
  97.         conn, addr = server.accept()
  98.         thread = threading.Thread(target=handle_client, args=(conn, addr))
  99.         thread.start()
  100.         print(f"[ACTIVE CONNETIONS] {threading.active_count() - 1}")
  101.  
  102. atexit.register(Exit)
  103. print("[STARTING] Server started")
  104. start()
  105.  
  106.  
  107.  
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement