Quellcode durchsuchen

Update 'client.py'

cube vor 1 Monat
Ursprung
Commit
65f844a929
1 geänderte Dateien mit 71 neuen und 71 gelöschten Zeilen
  1. 71
    71
      client.py

+ 71
- 71
client.py Datei anzeigen

@@ -1,72 +1,72 @@
1
-import socket, sys, datetime, threading, json
2
-
3
-class Client:
4
-    def __init__(self):
5
-        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
6
-        print("Created socket object")
7
-
8
-        self.client_name = "CLI"
9
-
10
-        self.server_ip = "cubes.link"
11
-        self.server_port = 1337
12
-        self.server_address = (self.server_ip, self.server_port)
13
-
14
-        a = {"header":"connection",
15
-             "client name":self.client_name}
16
-        self.send(a, self.server_address)
17
-
18
-    def send(self, data, addr):
19
-        data = json.dumps(data).encode()
20
-        self.sock.sendto(data, addr)
21
-
22
-    def run(self):
23
-        while True:
24
-            data, addr = self.sock.recvfrom(65507)
25
-            a = json.loads(data)
26
-            header = a["header"]
27
-
28
-            if header == "response":
29
-                message = a["message"]
30
-                client_name = a["client name"]
31
-                print(client_name + " >> " + message)
32
-            
33
-            elif header == "connection":
34
-                motd = a["motd"]
35
-                client_name = a["client name"]
36
-                print(">> " + client_name + " <<")
37
-
38
-class Commands:
39
-    def __init__(self):
40
-        self.commands={"test":self.test,
41
-                       "stop":self.stop}
42
-        self.running = True
43
-
44
-    def test(self, params):
45
-        print("this is a test command")
46
-
47
-    def stop(self, params):
48
-        print("this doesn't do anything rn")
49
-
50
-    def run(self):
51
-        while self.running:
52
-            text = input()
53
-            if text[0] == "/":
54
-                parts = text.split(" ")
55
-                command = parts[0].strip("/")
56
-                params = parts.pop(0)
57
-                if command in self.commands:
58
-                    self.commands[command](params)
59
-            else:
60
-                a = {"header":"message",
61
-                     "message":text,
62
-                     "client name":client.client_name}
63
-                client.send(a, client.server_address)
64
-
65
-client = Client()
66
-commands = Commands()
67
-
68
-client_thread = threading.Thread(target = client.run)
69
-commands_thread = threading.Thread(target = commands.run)
70
-
71
-client_thread.start()
1
+import socket, sys, datetime, threading, json
2
+
3
+class Client:
4
+    def __init__(self):
5
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
6
+        print("Created socket object")
7
+
8
+        self.client_name = "CLI"
9
+
10
+        self.server_ip = "cubes.link"
11
+        self.server_port = 1337
12
+        self.server_address = (self.server_ip, self.server_port)
13
+
14
+        a = {"header":"connection",
15
+             "client name":self.client_name}
16
+        self.send(a, self.server_address)
17
+
18
+    def send(self, data, addr):
19
+        data = json.dumps(data).encode()
20
+        self.sock.sendto(data, addr)
21
+
22
+    def run(self):
23
+        while True:
24
+            data, addr = self.sock.recvfrom(65507)
25
+            a = json.loads(data)
26
+            header = a["header"]
27
+
28
+            if header == "response":
29
+                message = a["message"]
30
+                client_name = a["client name"]
31
+                print(client_name + " >> " + message)
32
+            
33
+            elif header == "connection":
34
+                motd = a["motd"]
35
+                client_name = a["client name"]
36
+                print(">> " + client_name + " <<")
37
+
38
+class Commands:
39
+    def __init__(self):
40
+        self.commands={"test":self.test,
41
+                       "stop":self.stop}
42
+        self.running = True
43
+
44
+    def test(self, params):
45
+        print("this is a test command")
46
+
47
+    def stop(self, params):
48
+        print("this doesn't do anything rn")
49
+
50
+    def run(self):
51
+        while self.running:
52
+            text = input()
53
+            if text[0] == "/":
54
+                parts = text.split(" ")
55
+                command = parts[0].strip("/")
56
+                params = parts.pop(0)
57
+                if command in self.commands:
58
+                    self.commands[command](params)
59
+            else:
60
+                a = {"header":"message",
61
+                     "message":text,
62
+                     "client name":client.client_name}
63
+                client.send(a, client.server_address)
64
+
65
+client = Client()
66
+commands = Commands()
67
+
68
+client_thread = threading.Thread(target = client.run)
69
+commands_thread = threading.Thread(target = commands.run)
70
+
71
+client_thread.start()
72 72
 commands_thread.start()