-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileClient.py
59 lines (51 loc) · 2.05 KB
/
fileClient.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
import socket
import easygui
from Tkinter import Tk
# from tkFileDialog import askopenfilename
import tkFileDialog
def getfile():
Tk().withdraw()
filename = tkFileDialog.asksaveasfile()
s = str(filename)
lst = s.split("'")
return lst[1]
def Main():
host = easygui.enterbox(msg = 'Enter Server/Host IP:', title = 'HOST')
port = int(easygui.enterbox(msg = 'enter the port no to listen at: ',\
title = 'PORT'))
s = socket.socket()
s.connect((host,port))
#filename = raw_input('Filename? ->')
filename = easygui.enterbox(msg = 'FileName you want from the server and q in order to exit.'\
,title = 'FILENAME ?')
if filename != 'q':
s.send(filename)
data = s.recv(1024)
if data[:6] == 'EXISTS':
fileSize = long(data[6:])
#message = raw_input('File Exists, '+str(fileSize)+'Bytes, downoald?(Y/N)?')
message = easygui.enterbox(msg = 'File Exists, '+str(fileSize)+'Bytes, download?(Y/N)?',\
title = 'CONFIRM')
if message == 'Y':
s.send('OK')
# file_on_pc = filename
file_on_pc = getfile()
f = open(file_on_pc, 'wb')
data = s.recv(1024)
totalRecv = len(data)
f.write(data)
while totalRecv<fileSize:
data = s.recv(1024)
totalRecv += len(data)
f.write(data)
print '{0:.2f}'.format((totalRecv/float(fileSize))*100)+\
'% done'
print 'Downloading is Complete'
easygui.msgbox(msg = 'Downloading is complete', title = 'COMPLETE')
else:
easygui.messgbox(msg = 'File does not exist',title = 'ERROR')
print 'File does not exists'
s.send('CLOSE THE CONNECTION')
s.close()
if __name__=='__main__':
Main()