Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from customtkinter import *
- import socket
- import threading
- import json
- import atexit
- from os import getcwd
- from time import sleep
- HEADER = 1024
- PORT = 5050
- SERVER = socket.gethostbyname(socket.gethostname()) #"192.168.1.11"
- ADDR = (SERVER, PORT)
- DISCONNET_MESSAGE = "!DISCONNET"
- CWD = getcwd()
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server.bind(ADDR)
- attempts = {}
- file = open(f"{CWD}/players.json","r")
- json_data = json.load(file)
- ips = json_data["Ips"]
- file.close()
- player_data = json_data["Players"]
- def handle_client(conn, addr):
- global player_data, ips
- print(f"[NEW CONNETION] {addr} connected")
- loggedin = False
- ip = addr[0]
- print(ip, "--------------------------------------------------------------------")
- connected = True
- if ips[ip]["attempts"] < 1:
- print("Max", addr[0])
- sleep(5)
- maxAttempts = False
- ips[ip]["attempts"] = 3
- conn.send("LoginMessage:-Tryagain".encode("utf-8"))
- while connected:
- msg = conn.recv(HEADER).decode('utf-8')
- ##################################################################################
- if "Login:-" in msg: # handling login
- if not ips[ip]["attempts"] < 1:
- username, password = msg.split("Login:-")
- if username in player_data: # checking if the username is correct
- if password == player_data[username]["password"]:
- player = player_data[username]
- data = f"{player['username']}:-{player['balance']}:-Success:-LoginMessage".encode("utf-8")
- player_data[username]["Online"] = True
- conn.send(data)
- loggedin = True
- ips[ip]["attempts"] = 3
- break
- if not loggedin:
- if ips[ip]["attempts"] < 1:
- conn.send("Maxattempts:-LoginMessage".encode("utf-8"))
- print("Maxattempts")
- ips[ip]["attempts"] = 0
- print("attempts:",ips[ip]["attempts"])
- else:
- ips[ip]["attempts"] = ips[ip]["attempts"] - 1
- print("attempts:",ips[ip]["attempts"])
- conn.send(f"Denied:-{ips[ip]['attempts']}:-LoginMessage".encode("utf-8"))
- else:
- conn.send("Maxattempts".encode("utf-8"))
- maxAttempts = True
- ##################################################################################
- if msg == DISCONNET_MESSAGE:
- connected = False
- print(f"[CONNETION] {addr} disconnected")
- change(ips, player_data)
- conn.close()
- def change(ip, player):
- ips = ip
- player_data = player
- def Exit():
- file = open(f"{CWD}/players.json", "w+")
- data = {
- 'Players': player_data,
- 'Ips': ips
- }
- print(data)
- json.dump(data, file,indent=6)
- file.close()
- def start():
- global player_data, ips
- server.listen()
- print(f"SERVER IS listening on {SERVER}")
- while True:
- conn, addr = server.accept()
- thread = threading.Thread(target=handle_client, args=(conn, addr))
- thread.start()
- print(f"[ACTIVE CONNETIONS] {threading.active_count() - 1}")
- atexit.register(Exit)
- print("[STARTING] Server started")
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement