-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·63 lines (50 loc) · 1.55 KB
/
server.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
import socket
from _thread import *
import pickle
try:
with open('secret.pickle', 'rb') as reader:
secret = pickle.load(reader)
except:
pass
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((secret, port))
except socket.error as e:
print(e)
s.listen(2)
print("[SERVER STARTED] || Waiting for connection...")
players = [(110, 595, False, False, False, False, False, False, False, False, False, False, False, False, 400, False,
False, False, False, False, 0),
(1020, 595, False, False, False, False, False, False, False, False, False, False, False, False, 400, False,
False, False, False, False, 1)]
def threaded_client(conn, player):
global currentPlayer
conn.send(pickle.dumps(players[player]))
reply = ''
while True:
try:
data = pickle.loads(conn.recv(2048))
players[player] = data
if not data:
print("(Disconnected", addr)
break
else:
if player == 1:
reply = players[0]
else:
reply = players[1]
print("received >> ", data)
print("sending >> ", reply)
conn.sendall(pickle.dumps(reply))
except:
break
print("(LOST Connection)")
currentPlayer -= 1
conn.close()
currentPlayer = 0
while True:
conn, addr = s.accept()
print("(connected)", addr)
start_new_thread(threaded_client, (conn, currentPlayer))
currentPlayer += 1