-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_client.py
151 lines (134 loc) · 3.65 KB
/
dict_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python3
#coding=utf-8
from socket import *
import sys
import getpass
#创建网络连接
def main():
if len(sys.argv) < 3:
print("argv is error")
return
HOST = sys.argv[1]
PORT = int(sys.argv[2])
s = socket()
try:
s.connect((HOST,PORT))
except Exception as e:
print(e)
return
while True:
print('''
===========Welcome==========
-- 1.注册 2.登录 3.退出--
============================
''')
try:
cmd = int(input("输入选项>>"))
except Exception as e:
print("命令错误")
continue
if cmd not in [1,2,3]:
print("请输入正确选项")
sys.stdin.flush() #清除标准输入
continue
elif cmd == 1:
r = do_register(s)
if r == 0:
print("注册成功")
# login(s,name) #进入二级界面
elif r == 1:
print("用户存在")
else:
print("注册失败")
elif cmd == 2:
name = do_login(s)
if name:
print("登录成功")
login(s,name)
else:
print("用户名或密码不正确")
elif cmd == 3:
s.send(b'E')
sys.exit("谢谢使用")
def do_register(s):
while True:
name = input("User:")
passwd = getpass.getpass()
passwd1 = getpass.getpass('Again:')
if (' ' in name) or (' ' in passwd):
print("用户名和密码不许有空格")
continue
if passwd != passwd1:
print("两次密码不一致")
continue
#消息头表示什么请求
msg = 'R {} {}'.format(name,passwd)
#发送请求
s.send(msg.encode())
#等待回复
data = s.recv(128).decode()
if data == 'OK':
return 0
elif data == 'EXISTS':
return 1
else:
return 2
def do_login(s):
name = input("User:")
passwd = getpass.getpass()
msg = "L {} {}".format(name,passwd)
s.send(msg.encode())
data = s.recv(128).decode()
if data == 'OK':
return name
else:
return
def login(s,name):
while True:
print('''
==========查询界面==========
1.查词 2.历史记录 3.退出
===========================
''')
try:
cmd = int(input("输入选项>>"))
except Exception as e:
print("命令错误")
continue
if cmd not in [1,2,3]:
print("请输入正确选项")
sys.stdin.flush() #清除标准输入
continue
elif cmd == 1:
do_query(s,name)
elif cmd == 2:
do_hist(s,name)
elif cmd == 3:
return
def do_query(s,name):
while True:
word = input('单词:')
if word == '##':
break
msg = "Q {} {}".format(name,word)
s.send(msg.encode())
data = s.recv(128).decode()
if data == 'OK':
data = s.recv(2048).decode()
print(data)
else:
print("没有查到该单词")
def do_hist(s,name):
msg = 'H {}'.format(name)
s.send(msg.encode())
data = s.recv(128).decode()
if data == 'OK':
while True:
data = s.recv(1024).decode()
if data == '##':
break
print(data)
else:
print("没有历史记录")
if __name__ == '__main__':
main()