Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
596 lines (538 loc) · 26.6 KB
/
Copy pathclient.py
File metadata and controls
596 lines (538 loc) · 26.6 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import socket
import threading
import json
import time
import base64
import uuid
from tkinter import *
from tkinter import filedialog
from tkmacosx import Button
from PIL import Image, ImageTk
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
HOST = '127.0.0.1' # 主机地址
PORT = 8888 # 端口号
BUFFSIZE = 2048 # 缓存区大小,单位是字节,这里设定了2K的缓冲区
ADDR = (HOST, PORT) # 链接地址
count = 0
userlist = {}
grouplist = {}
image_fold_path = '/Users/rodrick/Documents/python/Chatroom/temp/img/'
file_fold_path = '/Users/rodrick/Documents/python/Chatroom/temp/file/'
class Client:
def __init__(self):
self.tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.isConnect = False
def connect(self):
"""连接服务器"""
if not self.isConnect:
self.tcpCliSock.connect(ADDR)
self.isConnect = True
print("连接成功")
else:
print("已经连接,不再重新连接")
def relogin(self):
"""重新加入聊天室"""
self.tcpCliSock.close()
self.__init__()
def disconnect(self):
"""断开服务器"""
self.tcpCliSock.close()
@staticmethod
def error_msg(info):
"""错误提示界面"""
errtk = Tk()
errtk.geometry('250x120')
errtk.title("错误")
frame = Frame(errtk, bg='#3C3F41')
frame.pack(expand=YES, fill=BOTH)
Label(frame, bg='#3C3F41', fg='#00FA92', text=info).pack(padx=5, pady=20, fill='x')
bt = Button(frame, text="确定", bg='#3C3F41', fg='#00FA92', command=errtk.destroy).pack()
errtk.mainloop()
class Login:
"""登录界面"""
def __init__(self, parent):
self.parent = parent
def login(self, entry, login_window):
"""登录"""
username = entry.get()
if username == "":
self.parent.error_msg("用户名不能为空")
return False
self.parent.username = username
try:
data = {"type": "login", "username": username}
json_data = json.dumps(data)
self.parent.connect()
except Exception as err:
print(err)
self.parent.error_msg("网络连接异常,无法连接到聊天室")
return False
else:
sock = self.parent.tcpCliSock
json_data = str.encode(json_data)
sock.send(json_data)
print('__send__' + str(json_data))
receive_json_data = sock.recv(BUFFSIZE)
print(receive_json_data)
receive_data = json.loads(receive_json_data, strict=False)
if receive_data["type"] == "login" and receive_data["username"] == username and \
receive_data["status"] == True:
# login success!
print("进入聊天室成功")
main_chat = self.parent.MainChat(self.parent, username)
login_window.destroy()
main_chat.__main__()
elif receive_data["type"] == "login" and receive_data["username"] == username \
and receive_data["info"] == "该用户名已被占用":
self.parent.relogin()
self.parent.error_msg("该用户名已被占用")
def login_window(self):
"""登录窗口"""
tk = Tk()
tk.geometry('350x200')
tk.title('登录')
frame = Frame(tk, bg='#3C3F41')
frame.pack(expand=YES, fill=BOTH)
Label(frame, bg='#3C3F41', fg="#00FA92", font="Arial, 15", text="请输入用户名:", anchor='w').pack(padx=20, pady=15, fill='x')
entry = Entry(frame, highlightthickness=0, highlightbackground='black', bg='#2B2B2B', fg='#00FA92', insertbackground='#00FA92', width=50, relief='flat')
entry.pack(padx=10, fill='x')
entry.bind("<Key-Return>", lambda x: self.login(entry, tk))
button = Button(frame, font="Arial, 15", bg='#3C3F41', fg='#00FA92', text="加入聊天室", command=lambda: self.login(entry, tk))
button.pack(pady=40)
tk.mainloop()
def __main__(self):
# 建立窗口
self.login_window()
class MainChat:
"""聊天室主窗口"""
def __init__(self, parent, username):
self.parent = parent
self.socket = parent.tcpCliSock
self.username = username
class MainWindow:
def __init__(self, parent):
self.parent = parent
def main_window(self):
"""聊天室主窗口"""
parent = self.parent
tk = Tk()
tk.geometry('600x450')
tk.title('Chat Room')
# 主背景
f = Frame(tk, bg='#3C3F41', width=600, height=450)
f.place(x=0, y=0)
# 欢迎语
text = StringVar()
text.set("欢迎来到聊天室, " + self.parent.username)
Label(f, fg="#00FA92", bg='#3C3F41', font="Courier, 14", textvariable=text).place(x=170, y=10, anchor=NW)
# 聊天的内容框
text_area = Text(f, highlightthickness=0, bg='#2B2B2B', fg="#00FA92", insertbackground='#00FA92', width=60, height=22, bd=0)
text_area.place(x=12, y=40, anchor=NW)
text_area.bind("<KeyPress>", lambda x: "break")
parent.text_area = text_area
# 右侧联系人选单
Label(f, fg="#00FA92", text="联系人:", bg="#3C3F41").place(x=455, y=10, anchor=NW)
listbox = Listbox(f, width=13, height=13, bg='#2B2B2B', fg="#00FA92")
listbox.place(x=460, y=40, anchor=NW)
parent.listbox = listbox
# 创建群聊
bt_create_group = Button(f, bg='#3C3F41', fg='#D636D2', width=100, text="创建多人聊天", font="Arial, 12", command=lambda: self.create_group_window())
bt_create_group.place(x=520, y=290, anchor=CENTER)
bt_clear = Button(f, bg='#3C3F41', fg='#DBBF6C', width=50, text="清屏", command=lambda: text_area.delete(0.0, END))
bt_clear.place(x=560, y=365, anchor=CENTER)
bt_pic = Button(f, bg='#3C3F41', fg='#D636D2', width=55, text="图片", font="Arial, 14", command=lambda: self.picture(parent.socket, chat_name))
bt_pic.place(x=110, y=405, anchor=CENTER)
bt_file = Button(f, bg='#3C3F41', fg='#D636D2', width=55, text="文件", font="Arial, 14", command=lambda: self.file(parent.socket, chat_name))
bt_file.place(x=180, y=405, anchor=CENTER)
# 下方内容输入
chat_name = Label(f, fg="#00FA92", text="群聊", font="Arial, 12", bg='#2B2B2B', width=8)
chat_name.place(x=12, y=352)
listbox.bind('<Double-Button-1>', lambda x: self.change_send_to(listbox, chat_name))
self.chat_name = chat_name
# 输入要发送的消息
et_input = Entry(f, highlightthickness=0, highlightbackground='black', width=37, bg='#2B2B2B', fg='#00FA92', insertbackground='#00FA92', borderwidth=0)
et_input.place(x=90, y=355)
et_input.bind('<Key-Return>', lambda x: self.send(parent.socket, chat_name, et_input))
self.et_input = et_input
# 发送消息
bt_send = Button(f, bg='#3C3F41', fg='#4FB6F9', width=50, text="发送", command=lambda: self.send(parent.socket, chat_name, et_input))
bt_send.place(x=480, y=365, anchor=CENTER)
tk.mainloop()
def create_group_window(self):
"""创建多人聊天"""
parent = self.parent
tk = Tk()
tk.geometry('200x300')
tk.title('创建多人聊天')
f = Frame(tk, bg='#3C3F41', width=200, height=300)
f.place(x=0, y=0)
listbox = Listbox(f, width=200, height=200, bg='#2B2B2B', fg="#00FA92", selectmode=MULTIPLE)
listbox.place(x=0, y=0, anchor=NW)
for l in userlist.keys():
listbox.insert(END, l)
listbox.pack()
bt_confirm = Button(f, bg='#3C3F41', fg='#DBBF6C', width=50, text="确定", command=lambda: self.group_name(listbox, tk))
bt_confirm.place(x=50, y=265, anchor=CENTER)
bt_cancel = Button(f, bg='#3C3F41', fg='#DBBF6C', width=50, text="取消", command=tk.destroy)
bt_cancel.place(x=140, y=265, anchor=CENTER)
tk.mainloop()
def group_name(self, listbox, parent_tk):
"""输入群聊名称"""
tk = Tk()
tk.geometry('350x200')
tk.title('输入群聊名称')
frame = Frame(tk, bg='#3C3F41')
frame.pack(expand=YES, fill=BOTH)
Label(frame, bg='#3C3F41', fg="#00FA92", font="Arial, 13", text="请输入群聊名称:", anchor='w').pack(padx=20,
pady=15,
fill='x')
entry = Entry(frame, highlightthickness=0, highlightbackground='black', bg='#2B2B2B', fg='#00FA92',
insertbackground='#00FA92', width=50, relief='flat')
entry.pack(padx=10, fill='x')
entry.bind("<Key-Return>", lambda x: self.create_group(entry, tk, listbox, parent_tk))
button = Button(frame, font="Arial, 13", bg='#3C3F41', fg='#00FA92', text="创建群聊",
command=lambda: self.create_group(entry, tk, listbox, parent_tk))
button.pack(pady=40)
tk.mainloop()
def change_send_to(self, listbox, chat_name):
"""双击选择聊天对象"""
try:
if self.parent.username != listbox.get(listbox.curselection()):
chat_name['text'] = listbox.get(listbox.curselection())
except Exception as err:
print(err)
pass # nothing choose
def create_group(self, entry, tk, listbox, parent_tk):
group_name = entry.get()
group_namelist = [self.parent.username]
send_grouplist = {}
try:
index_list = listbox.curselection()
if group_name == "":
group_name = self.parent.username
for index in range(len(index_list)):
print(index)
group_name += '、' + listbox.get(index)
group_namelist.append(listbox.get(index))
if group_name == self.parent.username: # 如果名字只有本人则说明没有选中其他用户
self.parent.parent.error_msg("未选中任何联系人")
return False
else:
group_name += '的群聊'
else:
if group_name not in grouplist:
if len(index_list) == 0:
self.parent.parent.error_msg("未选中任何联系人")
return False
for index in range(len(index_list)):
print(index)
group_namelist.append(listbox.get(index))
else:
self.parent.parent.error_msg("该群聊名称已存在")
return False
send_grouplist[group_name] = group_namelist
tk.destroy()
parent_tk.destroy()
except:
self.parent.parent.error_msg("未选中任何联系人")
data = {'type': 'create_group', 'group': send_grouplist, 'group_name': group_name, 'username': self.parent.username}
json_data = json.dumps(data)
json_data = str.encode(json_data)
self.parent.socket.send(json_data)
print('__send__' + str(json_data))
print(group_name)
print(group_namelist)
def picture(self, socket, chat_name):
root = Tk()
root.withdraw() # 不让选择文件对话框一直显示
filename = filedialog.askopenfilename(title='请选择发送的图片')
to_user = chat_name['text']
username = self.parent.username
if filename:
print(filename)
name = filename.split('/')[-1]
print(name)
print('Start uploading image!')
print('Waiting.......')
if to_user == '群聊':
data = {'type': 'group_pic', 'username': username}
json_data = json.dumps(data)
json_data = str.encode(json_data)
socket.send(json_data)
time.sleep(0.1)
print('__send__' + str(json_data))
message = 'group_pic '
socket.send(message.encode())
time.sleep(0.1)
print('Start uploading image!')
print('Waiting.......')
with open(filename, 'rb') as f:
while True:
send_data = f.read(BUFFSIZE)
print(send_data)
if not send_data:
break
socket.send(send_data)
time.sleep(0.1) # 延时确保文件发送完整
socket.send('EOF'.encode())
time.sleep(0.1)
print('Upload completed')
socket.send('quit'.encode())
time.sleep(0.1)
f.close()
else:
pic = Image.open(filename)
(x, y) = pic.size # read image size
if x > 350:
x_s = 350 # define standard width
y_s = int(y * x_s / x) # calc height based on standard width
pic = pic.resize((x_s, y_s), Image.ANTIALIAS) # resize image with high-quality
self.parent.pic_to_insert = ImageTk.PhotoImage(pic)
text_area = self.parent.text_area
t = "[" + to_user + ']\n'
text_area.insert(END, t)
text_area.image_create(END, image=self.parent.pic_to_insert)
text_area.insert(END, '\n')
text_area.see(END)
def file(self, socket, chat_name):
root = Tk()
root.withdraw() # 不让选择文件对话框一直显示
filename = filedialog.askopenfilename(title='请选择发送的文件')
to_user = chat_name['text']
username = self.parent.username
if filename:
print(filename)
name = filename.split('/')[-1]
print(name)
print('Start uploading file!')
print('Waiting.......')
if to_user == '群聊':
data = {'type': 'group_file', 'username': username, 'filename': name}
json_data = json.dumps(data)
json_data = str.encode(json_data)
socket.send(json_data)
time.sleep(0.1)
print('__send__' + str(json_data))
message = 'group_file'
socket.send(message.encode())
time.sleep(0.1)
print('Start uploading file!')
print('Waiting.......')
with open(filename, 'rb') as f:
while True:
send_data = f.read(BUFFSIZE)
print(send_data)
if not send_data:
break
socket.send(send_data)
time.sleep(0.1) # 延时确保文件发送完整
socket.send('EOF'.encode())
time.sleep(0.1)
print('Upload completed')
socket.send('quit'.encode())
time.sleep(0.1)
f.close()
def send(self, socket, chat_name, et_input):
"""点击发送按钮"""
text = et_input.get()
if text != "": # 发送消息不能为空
to_user = chat_name['text']
username = self.parent.username
print(to_user)
if to_user == '群聊':
data = {'type': 'group_chat', 'msg': text, 'username': username}
elif to_user in grouplist.keys():
data = {'type': 'create_group_chat', 'msg': text, 'username': username, 'to_list': grouplist[to_user], 'group_name': to_user}
else:
# 私聊
data = {'type': 'private_chat', 'msg': text, 'to': to_user, 'username': username}
text_area = self.parent.text_area
t = "[" + to_user + ']' + text + '\n'
text_area.insert(END, t)
json_data = json.dumps(data)
json_data = str.encode(json_data)
socket.send(json_data)
print('__send__' + str(json_data))
et_input.delete(0, END)
class ListenThread(threading.Thread):
"""Socket监听线程,对收到的信息作出相应反馈"""
def __init__(self, socket, parent):
threading.Thread.__init__(self)
self.parent = parent
self.socket = socket
self.recvd_size = 0
self.recvd_pic = ''
def run(self):
while True:
try:
receive_json_data = self.socket.recv(BUFFSIZE)
receive_data = json.loads(receive_json_data, strict=False)
except Exception as err:
print(err)
break
print("receive data: " + str(receive_data))
switch = {
"login": self.list,
"remove_user": self.list,
"group_chat": self.chat,
"private_chat": self.chat,
"create_group": self.list,
"create_group_chat": self.chat,
"group_pic": self.pic,
"group_file": self.file,
"pong": self.pong
}
switch[receive_data['type']](receive_data)
print("与服务器断开连接")
def list(self, data):
"""刷新列表"""
global count, userlist
if data['type'] == 'login' and count < 1:
time.sleep(0.1)
if count == 0:
text_area = self.parent.text_area
text = (data['username'] + "加入聊天室\n")
text_area.insert(END, text)
userlist[data['username']] = {}
data['type'] = 'init_list'
json_data = json.dumps(data)
json_data = str.encode(json_data)
self.socket.send(json_data)
print('send data: ' + str(json_data))
count += 1
elif data['type'] == 'remove_user':
text_area = self.parent.text_area
text = (data['username'] + "退出聊天室\n")
text_area.insert(END, text)
elif data['type'] == 'create_group':
grouplist[data['group_name']] = data['group']
listbox = self.parent.listbox
new_group = data['group_name']
listbox.insert(END, new_group) # 插入新列表
print(grouplist)
else:
count = 0
listbox = self.parent.listbox
list = ['群聊']
list += data['list']
listbox.delete(0, END) # 清空现有列表
userlist.clear()
for l in list:
listbox.insert(END, l) # 插入新列表
if str(l) != '群聊' and str(l) != self.parent.username:
userlist[l] = {}
for l in grouplist.keys():
listbox.insert(END, l) # 插入新列表
def chat(self, data):
"""接收聊天信息并打印"""
text_area = self.parent.text_area
if data ['type'] == 'group_chat':
text = ('[群聊]' + data['username'] + ': ' + data['msg'] + '\n')
elif data ['type'] == 'create_group_chat':
text = ('[' + data['group_name'] + ']' + data['username'] + ': ' + data['msg'] + '\n')
else:
text = ('[' + data['username'] + ']' + data['username'] + ': ' + data['msg'] + '\n')
text_area.insert(END, text)
text_area.see(END)
def pic(self, data):
sender = data['username']
if data['type'] == 'group_pic':
chat_type = 'group'
else:
chat_type = 'private'
print("hhh")
while True:
data = self.socket.recv(BUFFSIZE)
print(data)
data = data.decode()
print("begin receive picture")
if data == 'quit':
break
self.recv_pic(sender, chat_type)
def recv_pic(self, sender, chat_type):
"""接收图片并打印"""
name = str(uuid.uuid1()) # 获取文件名
file_path = image_fold_path + name # 将文件夹和图片名连接起来
print('Start saving!')
with open(file_path + '.png', 'wb+') as f:
while True:
data = self.socket.recv(BUFFSIZE)
print(data)
if data == 'EOF'.encode():
print('Saving completed!')
f.write(data)
break
f.write(data)
time.sleep(0.1)
f.close()
if chat_type == 'group':
pic = Image.open(file_path + '.png')
(x, y) = pic.size # read image size
if x > 350:
x_s = 350 # define standard width
y_s = int(y * x_s / x) # calc height based on standard width
pic = pic.resize((x_s, y_s), Image.ANTIALIAS) # resize image with high-quality
self.parent.pic_to_insert = ImageTk.PhotoImage(pic)
text_area = self.parent.text_area
t = '[群聊]' + sender + ':\n'
text_area.insert(END, t)
text_area.image_create(END, image=self.parent.pic_to_insert)
text_area.insert(END, '\n')
text_area.see(END)
def file(self, data):
sender = data['username']
filename = data['filename']
if data['type'] == 'group_file':
chat_type = 'group'
else:
chat_type = 'private'
print("hhh")
while True:
data = self.socket.recv(BUFFSIZE)
print(data)
data = data.decode()
print("begin receive file")
if data == 'quit':
break
self.recv_file(sender, chat_type, filename)
def recv_file(self, sender, chat_type, name):
"""接收文件"""
file_path = file_fold_path + name # 将文件夹和图片名连接起来
print('Start saving!')
with open(file_path, 'wb+') as f:
while True:
data = self.socket.recv(BUFFSIZE)
print(data)
if data == 'EOF'.encode():
print('Saving completed!')
f.write(data)
break
f.write(data)
time.sleep(0.1)
f.close()
if chat_type == 'group':
text_area = self.parent.text_area
t = '[群聊]' + sender + ': send file ' + name + '\n'
text_area.insert(END, t)
text_area.see(END)
def pong(self, data):
"""ping pong!"""
textArea = self.parent.textArea
text = '[Server]pong\n'
textArea.insert(END, text)
def __main__(self):
# 开启监听线程
listenThread = self.ListenThread(self.socket, self)
listenThread.daemon = True
listenThread.start()
self.listenThread = listenThread
window = self.MainWindow(self)
window.main_window()
self.window = window
def __main__(self):
login = Client.Login(self)
login.__main__()
if __name__ == '__main__':
client = Client()
client.__main__()