-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseServer.py
64 lines (51 loc) · 1.72 KB
/
databaseServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from _thread import start_new_thread
import socket
import pickle
import requests
server = "192.168.26.212"
port = 4000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
print(str(e))
s.listen()
print("Waiting for a connection, Server Started")
def send_login_request(username, password):
url = "http://localhost:8000/login" # Replace with your server's address and port
payload = {
"username": username,
"password": password
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(url, json=payload, headers=headers)
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
return response.json()
except requests.exceptions.RequestException as e:
print(f"HTTP Request failed: {e}")
return None
def threaded_client(conn):
while True:
try:
data = pickle.loads(conn.recv(1024))
if "stop" in data:
break
username = data["username"]
password = data["password"]
if username and password:
response = send_login_request(username, password)
print(f"Response from login route: {response}")
conn.send(pickle.dumps(response))
except Exception as e:
print(f"Error during communication with player: {e}")
break
print(f"Player lost connection")
conn.close()
print("[STARTING] server is starting...")
while True:
conn, addr = s.accept()
print("Connected to:", addr)
start_new_thread(threaded_client, (conn,))