-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.py
78 lines (65 loc) · 1.76 KB
/
client.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
客户端功能
"""
from socket import socket
from constants import PORT, SERVER
class Client:
""" 客户端 """
def __init__(self) -> None:
self.client = socket()
self.login = False
self.client.settimeout(3)
def connect(self) -> bool:
""" 连接服务端 """
out = self.client.connect_ex((SERVER, PORT))
return out == 0
def verify(self) -> bool | None:
""" 身份验证 """
try:
self.send(type='CLIENT')
back = self.recv()['type']
if back == 'SERVER':
return True
else:
self.close()
except:
return False
def close(self) -> None:
""" 断开连接 """
self.send(op='Quit')
self.client.close()
print(123)
def send(self, **kw) -> bool:
""" 发送信息 """
try:
self.client.send(kw.__repr__().encode('utf-8'))
return True
except:
return False
def recv(self) -> dict:
""" 接收信息 """
try:
data = self.client.recv(4096)
return eval(data)
except:
return {}
def run(self) -> None:
""" 开始运行 """
if self.connect():
print('CONNECT SUCCESS')
else:
print('ERROR IN CONNECT')
if self.verify():
print('VERIFY SUCCESS')
else:
print('ERROR IN VERIFY')
self.send(op='Mail', mail='[email protected]')
self.send(code=input('code:'), psd='2023', nick='Admin')
rtn = self.recv()
if rtn.get('act', None):
print(rtn['act'])
else:
print(rtn['info'])
self.close()
client = Client()
client.run()