-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.py
More file actions
69 lines (55 loc) · 2.03 KB
/
packet.py
File metadata and controls
69 lines (55 loc) · 2.03 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
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
64
65
66
67
68
69
import time
import json
def toPacket(jsonString):
temp = Packet()
temp.packetify(jsonString)
return temp
class Packet:
def __init__(self, packetType="", timestamp=time.time()):
super().__init__()
self.packet = {}
self.packet.update({'packetType':packetType, 'timestamp':timestamp})
def packetify(self, jsonString):
self.packet.clear()
self.packet.update(json.loads(jsonString))
def encode(self):
return json.dumps(self.packet, ensure_ascii=False).encode('utf-8')
def add(self, dict):
self.packet.update(dict)
def __repr__(self):
return json.dumps(self.packet, ensure_ascii=False)
class registerPacket(Packet):
def __init__(self, userID, userPass, userName):
super().__init__("register")
self.packet.update({'userID':userID,'userPass':userPass,'nickName':userName})
class loginPacket(Packet):
def __init__(self, userID, userPass):
super().__init__("login")
self.packet.update({'userID':userID,'userPass':userPass})
class alterPacket(Packet):
def __init__(self, userID, userPass, userName):
super().__init__("alter",time.time())
self.packet.update({'userID':userID,'userPass':userPass,'nickName':userName})
class msgPacket(Packet):
def __init__(self, text):
super().__init__("message", time.time())
self.packet.update({'text':text})
class cmdPacket(Packet):
def __init__(self, text):
super().__init__("command", time.time())
self.packet.update({'text':text})
class ChkPacket(Packet):
def __init__(self, bool, userName=None):
super().__init__("Chk", time.time())
self.packet.update({'Chk':bool, 'nickName':userName})
if __name__ == "__main__":
reg1 = registerPacket("user", "pass", "nickname")
log1 = loginPacket("user", "pass")
alt1 = alterPacket("user", "pass", "nickname")
msg1 = msgPacket("Hi! How are you?")
cmd1 = cmdPacket("/whoami")
print(reg1)
print(log1)
print(alt1)
print(msg1)
print(cmd1)