-
Notifications
You must be signed in to change notification settings - Fork 0
/
client3.py
223 lines (158 loc) · 6.86 KB
/
client3.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
#client
import tkinter as tk#采用Tkinter进行窗口视窗设计
import tkinter.messagebox
import pickle
import socket
import os
import hashlib
import sys
try:
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#生成socket连接对象
except:
print("400: Fail to create socket.")
sys.exit()
ip_port =("127.0.0.1", 6543)
try:
client.connect(ip_port)#进行连接
print("Succeed to connect to the server.")
except:
print("401: Problems in IP or port.")
sys.exit()
def get_func():
def get_files():
filename = file_name.get()
filedir = file_path.get()
content = "get" + " " + filename + " " + filedir
client.send(content.encode("utf-8"))
server_response = client.recv(1024)
file_size = int(server_response.decode("utf-8"))
if file_size==-1:
tkinter.messagebox.showerror('402', 'No such file in server.')
else:
#接受文件
client.send("Ready to recieve".encode("utf-8"))
f = open(filedir+'/'+filename, "wb")
received_size = 0
m = hashlib.md5()
while received_size < file_size:
size = 0
if file_size - received_size > 1024: #分批接收数据
size = 1024
else: #当为最后一次接收时
size = file_size - received_size
data = client.recv(size)
data_len = len(data)
received_size += data_len
m.update(data)
f.write(data)
f.close()
# md5值校验
md5_sever = client.recv(1024).decode("utf-8")
md5_client = m.hexdigest()
if md5_sever == md5_client:
mes = 'Header received:' + str(file_size) + " Size actually recieved:" + str(received_size) + " MD5 check pass"
tkinter.messagebox.showinfo('Successfully Get!', mes)
else:
tkinter.messagebox.showerror('Error', 'MD5 check failed.')
window_get.destroy()
#定义窗口上的窗口
window_get = tk.Toplevel(window)
window_get.geometry('300x200')
window_get.title('Get Files from Server')#窗口标题——用于展示功能
file_name = tk.StringVar() #将输入的注册名赋值给变量
tk.Label(window_get, text='File Name: ').place(x=10, y=10) # 将file name放置在坐标(10,10)处
entry_file_name = tk.Entry(window_get, textvariable=file_name) # 创建一个注册名的entry,变量为new_name
entry_file_name.place(x=130, y=10)
file_path = tk.StringVar()
tk.Label(window_get, text='File Path: ').place(x=10, y=50)
entry_file_path = tk.Entry(window_get, textvariable=file_path)
entry_file_path.place(x=130, y=50)
btn_comfirm = tk.Button(window_get, text='OK', command=get_files)
btn_comfirm.place(x=180, y=120)
def put_func():
def put_files():
filedir = file_path.get()
content = "put" + " " + filedir
if os.path.isfile(filedir):
client.send(content.encode("utf-8"))
header = os.stat(filedir).st_size
client.send(str(header).encode("utf-8"))
client.recv(1024)
m = hashlib.md5()
f = open(filedir, "rb")
for line in f:
client.send(line)
m.update(line)
f.close()
# 发送md5值进行校验
md5 = m.hexdigest()
client.send(md5.encode("utf-8"))
server_response = client.recv(1024)
tkinter.messagebox.showinfo('OK', str(server_response.decode("utf-8")))
else:
tkinter.messagebox.showerror('403', 'No such file in client')
window_put.destroy()
window_put = tk.Toplevel(window)
window_put.geometry('300x200')
window_put.title('Put Files to Server')#窗口标题——用于展示功能
file_path = tk.StringVar()
tk.Label(window_put, text='File Path: ').place(x=10, y=50)
entry_file_path = tk.Entry(window_put, textvariable=file_path)
entry_file_path.place(x=130, y=50)
btn_comfirm = tk.Button(window_put, text='OK', command=put_files)
btn_comfirm.place(x=180, y=120)
def ask_dir_func():
def ask_dir_of_files():
filename = file_name.get()
content = "ask_dir" + " " + filename
client.send(content.encode("utf-8"))
server_response = client.recv(1024)
filedir = str(server_response.decode("utf-8"))
if filedir == "No such file":
tkinter.messagebox.showerror('404', 'No such file in server or no right to visit its directory')
print ("404: No such file in server or no right to visit its directory")
elif len(filename) !=0:
a = "The directory of " + filename + " is " + filedir
print (a)
tkinter.messagebox.showinfo('OK', a)
else:
a = "The directories and files of current path are: " + filedir
tkinter.messagebox.showinfo('OK', a)
window_ask.destroy()
window_ask = tk.Toplevel(window)
window_ask.geometry('300x200')
window_ask.title('List All Directories or Ask File Directory')#窗口标题——用于展示功能
file_name = tk.StringVar()
tk.Label(window_ask, text='File Name: ').place(x=10, y=50)
entry_file_path = tk.Entry(window_ask, textvariable=file_name)
entry_file_path.place(x=130, y=50)
btn_comfirm = tk.Button(window_ask, text='OK', command=ask_dir_of_files)
btn_comfirm.place(x=180, y=120)
if True:
#建立图形界面窗口
window = tk.Tk()
#给可视化窗口起名
window.title('客户与服务器间的文件传输协议')
#设置窗口的大小
window.geometry('500x300')#需要注意的是,这里的x为字母x
#向窗口中插入背景图片
canvas = tk.Canvas(window, width=500, height=200)
image_file = tk.PhotoImage(file='background.gif')
image = canvas.create_image(250, 0, anchor='n',image=image_file)
canvas.pack(side='top')
tk.Label(window, text='请根据需求选择以下功能',font=('微软雅黑', 15)).pack()
#展示本文件传输协议的三大功能,用户通过鼠标按键选择对应功能
btn_get = tk.Button(window, text='获取文件(get)', command=get_func, bg='lightcyan')
btn_get.place(x=100, y=240)
btn_put = tk.Button(window, text='存放文件(put)', command=put_func, bg='lightcyan')
btn_put.place(x=200, y=240)
btn_ask = tk.Button(window, text = '查询文件目录(ask_dir)', command=ask_dir_func, bg='lightcyan')
btn_ask.place(x=300, y=240)
#窗口循环显示,这样用户才能够多次操作
window.mainloop()
client.close()
# In[ ]:
# In[ ]: