-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
48 lines (40 loc) · 1.53 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
import socketserver
import socket
class Connection:
def __init__(self, name, ip, port):
self.name = name
self.ip = ip
self.port = port
def __str__(self):
return self.name + "\r\n\t" + self.ip + "\r\n\t" + self.port
class GPSHandler(socketserver.BaseRequestHandler):
opengames = []
def handle(self):
self.data = self.request.recv(1024).strip()
conninfo = self.data.decode("utf-8").split(',')
print(conninfo[0])
if conninfo[0] == "START":
newconn = Connection(conninfo[1], conninfo[2], conninfo[3])
self.opengames.append(newconn)
self.opengames.sort(key=lambda connection: connection.name)
print("==========")
for conn in self.opengames:
print(conn.name)
print("==========")
self.request.sendall(bytes("Received", "utf-8"))
elif conninfo[0] == "GET":
retstr = ""
for conn in self.opengames:
retstr += conn.name + ','
self.request.sendall(bytes(retstr[:-1], "utf-8"))
else:
selgame = self.opengames[int(conninfo[1])]
self.opengames.remove(selgame)
retstr = selgame.name + "," + selgame.ip + "," + selgame.port
print("Game to get: " + conninfo[1])
self.request.sendall(bytes(retstr, "utf-8"))
if __name__ == "__main__":
#print("hi")
HOST, PORT = '', 8000
server = socketserver.TCPServer((HOST, PORT), GPSHandler)
server.serve_forever()