|
|
@@ -1,91 +1,91 @@
|
|
1
|
|
-import socket, sys, datetime, threading, json
|
|
2
|
|
-
|
|
3
|
|
-class Server:
|
|
4
|
|
- def __init__(self):
|
|
5
|
|
- self.version = 1.0
|
|
6
|
|
- print("Server version: {}".format(self.version))
|
|
7
|
|
- self.motd = "cubey"
|
|
8
|
|
- self.color = 0xe973ea
|
|
9
|
|
- self.users = []
|
|
10
|
|
-
|
|
11
|
|
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
12
|
|
- print("Created socket object")
|
|
13
|
|
-
|
|
14
|
|
- self.ip = "0.0.0.0"
|
|
15
|
|
- self.port = 1337
|
|
16
|
|
- self.address = (self.ip, self.port)
|
|
17
|
|
- self.sock.bind(self.address)
|
|
18
|
|
- print("Server started up on {}".format(self.address))
|
|
19
|
|
-
|
|
20
|
|
- def send(self, data, client):
|
|
21
|
|
- data = json.dumps(data).encode()
|
|
22
|
|
- self.sock.sendto(data, client)
|
|
23
|
|
-
|
|
24
|
|
- def send_all(self, data):
|
|
25
|
|
- for user in self.users:
|
|
26
|
|
- self.send(data, user)
|
|
27
|
|
-
|
|
28
|
|
- def connection(self, address, connecting_client_name):
|
|
29
|
|
- print("New connection from {}".format(address))
|
|
30
|
|
- a = {"header":"connection",
|
|
31
|
|
- "motd":self.motd,
|
|
32
|
|
- "color":self.color,
|
|
33
|
|
- "client name":connecting_client_name}
|
|
34
|
|
- print(">> " + connecting_client_name + " <<")
|
|
35
|
|
- self.users.append(address)
|
|
36
|
|
- self.send_all(a)
|
|
37
|
|
-
|
|
38
|
|
- def run(self):
|
|
39
|
|
- while True:
|
|
40
|
|
- try:
|
|
41
|
|
- data, addr = self.sock.recvfrom(65507)
|
|
42
|
|
- a = json.loads(data)
|
|
43
|
|
- header = a["header"]
|
|
44
|
|
- client_name = a["client name"]
|
|
45
|
|
-
|
|
46
|
|
- if header == "connection":
|
|
47
|
|
- self.connection(addr, client_name)
|
|
48
|
|
- elif header == "message":
|
|
49
|
|
- message = a["message"]
|
|
50
|
|
- print(client_name + " >> " + message)
|
|
51
|
|
- response = {"header":"response",
|
|
52
|
|
- "message":message,
|
|
53
|
|
- "color":0xffffff,
|
|
54
|
|
- "client name":client_name}
|
|
55
|
|
- if addr not in self.users:
|
|
56
|
|
- self.users.append(addr)
|
|
57
|
|
- self.send_all(response)
|
|
58
|
|
- except OSError:
|
|
59
|
|
- return
|
|
60
|
|
-
|
|
61
|
|
-class Commands:
|
|
62
|
|
- def __init__(self):
|
|
63
|
|
- self.commands={"test":self.test,
|
|
64
|
|
- "stop":self.stop}
|
|
65
|
|
- self.running = True
|
|
66
|
|
-
|
|
67
|
|
- def test(self):
|
|
68
|
|
- print("this is a test command")
|
|
69
|
|
-
|
|
70
|
|
- def stop(self):
|
|
71
|
|
- print("Stopping server...")
|
|
72
|
|
- server.sock.close()
|
|
73
|
|
- server_thread.join()
|
|
74
|
|
- self.running = False
|
|
75
|
|
-
|
|
76
|
|
- def run(self):
|
|
77
|
|
- while self.running:
|
|
78
|
|
- command = input()
|
|
79
|
|
- if command in self.commands:
|
|
80
|
|
- self.commands[command]()
|
|
81
|
|
- else:
|
|
82
|
|
- print("Unknown command")
|
|
83
|
|
-
|
|
84
|
|
-server = Server()
|
|
85
|
|
-commands = Commands()
|
|
86
|
|
-
|
|
87
|
|
-server_thread = threading.Thread(target = server.run)
|
|
88
|
|
-commands_thread = threading.Thread(target = commands.run)
|
|
89
|
|
-
|
|
90
|
|
-server_thread.start()
|
|
|
1
|
+import socket, sys, datetime, threading, json
|
|
|
2
|
+
|
|
|
3
|
+class Server:
|
|
|
4
|
+ def __init__(self):
|
|
|
5
|
+ self.version = 1.0
|
|
|
6
|
+ print("Server version: {}".format(self.version))
|
|
|
7
|
+ self.motd = "cubey"
|
|
|
8
|
+ self.color = 0xe973ea
|
|
|
9
|
+ self.users = []
|
|
|
10
|
+
|
|
|
11
|
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
12
|
+ print("Created socket object")
|
|
|
13
|
+
|
|
|
14
|
+ self.ip = "0.0.0.0"
|
|
|
15
|
+ self.port = 1337
|
|
|
16
|
+ self.address = (self.ip, self.port)
|
|
|
17
|
+ self.sock.bind(self.address)
|
|
|
18
|
+ print("Server started up on {}".format(self.address))
|
|
|
19
|
+
|
|
|
20
|
+ def send(self, data, client):
|
|
|
21
|
+ data = json.dumps(data).encode()
|
|
|
22
|
+ self.sock.sendto(data, client)
|
|
|
23
|
+
|
|
|
24
|
+ def send_all(self, data):
|
|
|
25
|
+ for user in self.users:
|
|
|
26
|
+ self.send(data, user)
|
|
|
27
|
+
|
|
|
28
|
+ def connection(self, address, connecting_client_name):
|
|
|
29
|
+ print("New connection from {}".format(address))
|
|
|
30
|
+ a = {"header":"connection",
|
|
|
31
|
+ "motd":self.motd,
|
|
|
32
|
+ "color":self.color,
|
|
|
33
|
+ "client name":connecting_client_name}
|
|
|
34
|
+ print(">> " + connecting_client_name + " <<")
|
|
|
35
|
+ self.users.append(address)
|
|
|
36
|
+ self.send_all(a)
|
|
|
37
|
+
|
|
|
38
|
+ def run(self):
|
|
|
39
|
+ while True:
|
|
|
40
|
+ try:
|
|
|
41
|
+ data, addr = self.sock.recvfrom(65507)
|
|
|
42
|
+ a = json.loads(data)
|
|
|
43
|
+ header = a["header"]
|
|
|
44
|
+ client_name = a["client name"]
|
|
|
45
|
+
|
|
|
46
|
+ if header == "connection":
|
|
|
47
|
+ self.connection(addr, client_name)
|
|
|
48
|
+ elif header == "message":
|
|
|
49
|
+ message = a["message"]
|
|
|
50
|
+ print(client_name + " >> " + message)
|
|
|
51
|
+ response = {"header":"response",
|
|
|
52
|
+ "message":message,
|
|
|
53
|
+ "color":0xffffff,
|
|
|
54
|
+ "client name":client_name}
|
|
|
55
|
+ if addr not in self.users:
|
|
|
56
|
+ self.users.append(addr)
|
|
|
57
|
+ self.send_all(response)
|
|
|
58
|
+ except OSError:
|
|
|
59
|
+ return
|
|
|
60
|
+
|
|
|
61
|
+class Commands:
|
|
|
62
|
+ def __init__(self):
|
|
|
63
|
+ self.commands={"test":self.test,
|
|
|
64
|
+ "stop":self.stop}
|
|
|
65
|
+ self.running = True
|
|
|
66
|
+
|
|
|
67
|
+ def test(self):
|
|
|
68
|
+ print("this is a test command")
|
|
|
69
|
+
|
|
|
70
|
+ def stop(self):
|
|
|
71
|
+ print("Stopping server...")
|
|
|
72
|
+ server.sock.close()
|
|
|
73
|
+ server_thread.join()
|
|
|
74
|
+ self.running = False
|
|
|
75
|
+
|
|
|
76
|
+ def run(self):
|
|
|
77
|
+ while self.running:
|
|
|
78
|
+ command = input()
|
|
|
79
|
+ if command in self.commands:
|
|
|
80
|
+ self.commands[command]()
|
|
|
81
|
+ else:
|
|
|
82
|
+ print("Unknown command")
|
|
|
83
|
+
|
|
|
84
|
+server = Server()
|
|
|
85
|
+commands = Commands()
|
|
|
86
|
+
|
|
|
87
|
+server_thread = threading.Thread(target = server.run)
|
|
|
88
|
+commands_thread = threading.Thread(target = commands.run)
|
|
|
89
|
+
|
|
|
90
|
+server_thread.start()
|
|
91
|
91
|
commands_thread.start()
|