-
Notifications
You must be signed in to change notification settings - Fork 0
/
testclient.py
341 lines (268 loc) · 10.1 KB
/
testclient.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
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
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
import tkMessageBox
import ttk
import tkFont as font
from Tkinter import *
from tkFileDialog import askopenfilename
import Tkconstants
from MyQueue import node
from MyQueue import queue
import socket
import os
import threading
import shutil
import zipfile
LARGE_FONT= ("Verdana", 12)
s = socket.socket()
def Bind():
host = '127.0.0.1'
port = 6200
global s
s.connect((host, port))
def walk_dir(root_dir):
"""
walks the specified directory root and all its subdirectories
and returns a list of tuples containing all files with extension ext
and their associated directory
"""
file_list = []
towalk = [root_dir]
while towalk:
root_dir = towalk.pop()
for path in os.listdir(root_dir):
full_path = os.path.join(root_dir, path).lower()
if os.path.isfile(full_path) and (full_path.endswith(extension) or full_path.endswith(extension2) or full_path.endswith(extension3) or full_path.endswith(extension4) or full_path.endswith(extension5) or full_path.endswith(extension6) or full_path.endswith(extension7)):
# create list of (filename, dir) tuples
file_list.append((path.lower(), root_dir))
elif os.path.isdir(full_path):
towalk.append(full_path)
return file_list
def get_list(event):
"""
function to read the listbox selection(s)
(mutliple lines can be selected with ctrl/shift mouse clicks)
and put the result(s) in a label
"""
try:
global selpath
# tuple of line index(es)
sel = listbox1.curselection()
# get the text, might be multi-line
selpath = [file_list[int(x)] for x in sel]
except:
info_label.config(text="Please select a file on the list")
def add_file():
"""
add selected file(s) from listbox1 to listbox2
keep track of the (filename, dir) tuple too
"""
try:
for path_tuple in selpath:
listbox2.insert(END, path_tuple[0])
# this list exists parallel to the listbox2
# and contains the associated directory info
fullpath_list.append(path_tuple)
#print ">>>", fullpath_list #test
except NameError:
info_label.config(text="Please select a file on the list")
def quit():
global app
app.destroy()
#s.send('N')
s.close()
class View(Listbox):
def __init__(self, master):
Listbox.__init__(self, master)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (mainpage, uploadpage,downloadpage):
frame = F(container, self)
frame.configure(background = "black")
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="news")
self.show_frame(mainpage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class mainpage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
#font11 = font.Font(family='Helvetica', size=15, weight = 'bold')
font12 = font.Font(family='Helvetica', size=15)
label = tk.Label(self, text="Welcome to Jalebi File Transfer software.", fg = 'green', bg = 'black')
label.place(x=50, y=20)
Bind()
button = tk.Button(self, text="Upload", activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue",command=lambda: controller.show_frame(uploadpage) )
button.place(x=200, y=80)
button2 = tk.Button(self, text="Download", activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue",command=lambda: controller.show_frame(downloadpage))
button2.place(x=190, y=150)
button3 = tk.Button(self, text="Quit", activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue", command= quit)
button3.place(x=215, y=220)
font11 = font.Font(family='Helvetica', size=15, weight='bold')
button['font'] = font11
button2['font'] = font11
label['font'] = font11
button3['font'] = font11
class uploadpage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label1 = tk.Label(self, text="Select the file you want to upload", fg = 'green', bg = 'black')
label1.place(x=100, y=60)
font12 = font.Font(family='Helvetica', size=12)
label1['font'] = font12
button = tk.Button(self, text="Choose file", activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue", command = self.browse)
button.place(x=180, y=110)
button['font'] = font12
button = tk.Button(self, text="Back", activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue",command=lambda: controller.show_frame(mainpage))
button.place(x=50, y=160)
button['font'] = font12
button = tk.Button(self, text="Upload", activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue",command=self.Upfile)
button.place(x=350, y=160)
button['font'] = font12
self.listbox1 = Listbox(self, width=70, height=6, selectmode=EXTENDED)
self.listbox1.grid(padx= 30, pady= 210)
self.name = ""
self.filesize = 0
self.totalSent = 0
def browse(self):
self.name= askopenfilename()
if self.name == "":
return
self.listbox1.delete(0, END)
(head, tail) = os.path.split(self.name)
self.filesize = str(os.path.getsize(self.name))
self.listbox1.insert(END, "Filename: " + tail)
self.listbox1.insert(END, "Filesize: " + self.filesize)
(root, ext) = os.path.splitext(tail)
s.send('1')
if ext != ".zip":
s.send(tail.replace(ext, ".zip"))
else:
s.send(tail)
def Upfile(self):
(head, tail) = os.path.split(self.name)
(root, ext) = os.path.splitext(tail)
if ext != ".zip":
print "Original filesize: " + self.filesize
os.mkdir("temp")
zip_folder = tail.replace(ext, "")
name = tail.replace(ext, ".zip")
file1 = open("C:\Users\M.Ali\Desktop\Project\\final\\Client\\temp\\" + str(tail), "wb")
file2 = open(self.name, "rb")
shutil.copyfileobj(file2, file1)
file1.close()
file2.close()
shutil.make_archive(zip_folder, "zip", "temp")
os.remove("C:\Users\M.Ali\Desktop\Project\\final\\Client\\temp\\" + str(tail))
os.rmdir("temp")
filesize = str(os.path.getsize(name))
print "Compressed filesize: " + filesize
s.send(str(os.path.getsize(name)))
self.filesize = os.path.getsize(name)
file2 = open(name, "rb")
else:
s.send(self.filesize)
file2 = open(self.name, "rb")
self.totalSent = 0
upqueue = queue()
bytesToSend = file2.read(1024)
upqueue.Enqueue(bytesToSend)
self.totalSent = len(bytesToSend)
while bytesToSend != "":
bytesToSend = file2.read(1024)
upqueue.Enqueue(bytesToSend)
self.totalSent += len(bytesToSend)
self.listbox1.insert(END, "{0:.2f}".format((self.totalSent/float(self.filesize)*100))+ "% Done")
self.listbox1.yview(END)
self.listbox1.insert(END, "Upload complete")
self.listbox1.yview(END)
file2.close()
for x in range(0, upqueue.Count()):
s.send(upqueue.Dequeue())
class downloadpage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
font12 = font.Font(family='Helvetica', size=12)
font13 = font.Font(family='Helvetica', size=14)
label1 = tk.Label(self, text="Select the file you want to download", fg = 'green', bg = 'black')
label1.place(x=10, y=10)
label1['font'] = font13
self.listbox1 = Listbox(self, width=60, height=14, selectmode=SINGLE)
self.listbox1.grid(padx= 30, pady= 50)
add_button = Button(self, text='Download file',activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue", command=self.DownFile)
add_button.place(x=300, y=300)
add_button['font'] = font12
add_button = Button(self, text='Retrieve file',activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue", command=self.RetrieveFile)
add_button.place(x=130, y=300)
add_button['font'] = font12
add_button = Button(self, text='Back',activebackground="Blue", activeforeground="yellow", fg = "red", highlightcolor = "blue", command=lambda: controller.show_frame(mainpage))
add_button.place(x=30, y=300)
add_button['font'] = font12
self.value = ""
def CurSelet(self, evt):
self.value = str(self.listbox1.get(self.listbox1.curselection()))
newstr = self.value.replace("\n", "")
print newstr
def RetrieveFile(self):
s.send('2')
RecvFile = s.recv(1024)
f2 = open("files.txt", "w")
f2.write(RecvFile)
f2.close()
self.listbox1.delete(0, END)
file_list = open("files.txt").readlines()
for file in file_list:
self.listbox1.insert(END, file)
file_num = len(file_list)
self.listbox1.bind('<<ListboxSelect>>', self.CurSelet)
def DownFile(self):
newstr = self.value.replace("\n", "")
s.send(newstr)
fsize = s.recv(1024)
print "Compressed filesize: " + fsize
data = s.recv(1024)
downqueue = queue()
downqueue.Enqueue(data)
totalRecv = len(data)
self.listbox1.delete(0, END)
while totalRecv < long(fsize):
data = s.recv(1024)
totalRecv += len(data)
downqueue.Enqueue(data)
self.listbox1.insert(END, "{0:.2f}".format((totalRecv/float(fsize))*100)+ "% Done")
self.listbox1.yview(END)
self.listbox1.insert(END, "Download Complete!")
self.listbox1.yview(END)
(root, ext) = os.path.splitext(newstr)
if ext != ".zip":
name = newstr.replace(ext, ".zip")
f = open(name, 'wb')
else:
f = open(newstr, "wb")
for x in range(0, downqueue.Count()):
f.write(downqueue.Dequeue())
f.close()
zfile = zipfile.ZipFile(newstr.replace(ext, ".zip"))
zfile.extractall()
zfile.close()
os.remove(newstr.replace(ext, ".zip"))
print "Decompressed filesize: " + str(os.path.getsize(newstr))
app = SeaofBTCapp()
w = 470
h = 350
x = 100
y = 200
app.geometry("%dx%d+%d+%d" % (w, h, x, y))
app.title("Jalebi File Transfer Software")
app.mainloop()