-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.py
203 lines (185 loc) · 6.07 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#coding:utf-8
'''
file:client.py.py
date:2017/9/11 11:01
author:lockey
email:lockey@123.com
platform:win7.x86_64 pycharm python3
desc:p2p communication clientside
'''
from socket import *
import threading,sys,json,re
HOST = '192.168.1.7' ##
PORT=8022
BUFSIZE = 1024 ##缓冲区大小 1K
ADDR = (HOST,PORT)
myre = r"^[_a-zA-Z]\w{0,}"
tcpCliSock = socket(AF_INET,SOCK_STREAM)
userAccount = None
def register():
print("""
Glad to have you a member of us!
""")
accout = input('Please input your account: ')
if not re.findall(myre, accout):
print('Account illegal!')
return None
password1 = input('Please input your password: ')
password2 = input('Please confirm your password: ')
if not (password1 and password1 == password2):
print('Password not illegal!')
return None
global userAccount
userAccount = accout
regInfo = [accout,password1,'register']
datastr = json.dumps(regInfo)
tcpCliSock.send(datastr.encode('utf-8'))
data = tcpCliSock.recv(BUFSIZE)
data = data.decode('utf-8')
if data == '0':
print('Success to register!')
return True
elif data == '1':
print('Failed to register, account existed!')
return False
else:
print('Failed for exceptions!')
return False
def login():
print("""
Welcome to login in!
""")
accout = input('Account: ')
if not re.findall(myre, accout):
print('Account illegal!')
return None
password = input('Password: ')
if not password:
print('Password illegal!')
return None
global userAccount
userAccount = accout
loginInfo = [accout, password,'login']
datastr = json.dumps(loginInfo)
tcpCliSock.send(datastr.encode('utf-8'))
data = tcpCliSock.recv(BUFSIZE)
if data == '0':
print('Success to login!')
return True
else:
print('Failed to login in(user not exist or username not match the password)!')
return False
def addGroup():
groupname = input('Please input group name: ')
if not re.findall(myre, groupname):
print('group name illegal!')
return None
return groupname
def chat(target):
while True:
print('{} -> {}: '.format(userAccount,target))
msg = input()
if len(msg) > 0 and not msg in 'qQ':
if 'group' in target:
optype = 'cg'
else:
optype = 'cp'
dataObj = {'type': optype, 'to': target, 'msg': msg, 'froms': userAccount}
datastr = json.dumps(dataObj)
tcpCliSock.send(datastr.encode('utf-8'))
continue
elif msg in 'qQ':
break
else:
print('Send data illegal!')
class inputdata(threading.Thread):
def run(self):
menu = """
(CP): Chat with individual
(CG): Chat with group member
(AG): Add a group
(EG): Enter a group
(H): For help menu
(Q): Quit the system
"""
print(menu)
while True:
operation = input('Please input your operation("h" for help): ')
if operation in 'cPCPCpcp':
target = input('Who would you like to chat with: ')
chat(target)
continue
if operation in 'cgCGCgcG':
target = input('Which group would you like to chat with: ')
chat('group'+target)
continue
if operation in 'agAGAgaG':
groupName = addGroup()
if groupName:
dataObj = {'type': 'ag', 'groupName': groupName}
dataObj = json.dumps(dataObj)
tcpCliSock.send(dataObj.encode('utf-8'))
continue
if operation in 'egEGEgeG':
groupname = input('Please input group name fro entering: ')
if not re.findall(myre, groupname):
print('group name illegal!')
return None
dataObj = {'type': 'eg', 'groupName': 'group'+groupname}
dataObj = json.dumps(dataObj)
tcpCliSock.send(dataObj.encode('utf-8'))
continue
if operation in 'hH':
print(menu)
continue
if operation in 'qQ':
sys.exit(1)
else:
print('No such operation!')
class getdata(threading.Thread):
def run(self):
while True:
data = tcpCliSock.recv(BUFSIZE).decode('utf-8')
if data == '-1':
print('can not connect to target!')
continue
if data == 'ag0':
print('Group added!')
continue
if data == 'eg0':
print('Entered group!')
continue
if data == 'eg1':
print('Failed to enter group!')
continue
dataObj = json.loads(data)
if dataObj['type'] == 'cg':
print('{}(from {})-> : {}'.format(dataObj['froms'], dataObj['to'], dataObj['msg']))
else:
print('{} ->{} : {}'.format(dataObj['froms'], userAccount, dataObj['msg']))
def main():
try:
tcpCliSock.connect(ADDR)
print('Connected with server')
while True:
loginorReg = input('(l)ogin or (r)egister a new account: ')
if loginorReg in 'lL':
log = login()
if log:
break
if loginorReg in 'rR':
reg = register()
if reg:
break
myinputd = inputdata()
mygetdata = getdata()
myinputd.start()
mygetdata.start()
myinputd.join()
mygetdata.join()
except Exception:
print('error')
tcpCliSock.close()
sys.exit()
if __name__ == '__main__':
main()