-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
37 lines (32 loc) · 1.04 KB
/
Client.py
File metadata and controls
37 lines (32 loc) · 1.04 KB
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
import socket
import json
import time
import threading
class Client:
def __init__(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect(("127.0.0.1", 1660))
self.readTask = threading.Thread(target=self.ReadLoop, args=())
self.readTask.start()
def send(self, data):
data = json.dumps(data).encode()
self.s.send(data)
def ReadLoop(self):
while 1:
time.sleep(0.4)
data = self.s.recv(1024).decode()
for d in data.split("}"):
if d == "":
continue
jd = json.loads(d+"}")
if jd["Type"] == "Message":
Name = jd["Name"]
Message = jd["Message"]
print(Name + "> " + Message)
c = Client()
while 1:
Text = input("")
if Text.split(" ")[0].lower() == "!nick":
c.send({"Type": "Nick", "Value": Text.replace("!nick ", "").replace("!Nick ", "")})
else:
c.send({"Type": "Message", "Value": Text})