-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.py
More file actions
75 lines (63 loc) · 2.22 KB
/
client.py
File metadata and controls
75 lines (63 loc) · 2.22 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
import socket
from tkinter import Tk,filedialog
temp_file,temp_socket = None, None
def receiver():
global temp_file,temp_socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
temp_socket = s
port = 54321
host = input('Enter Sender IP: ')
## CONNECTION
while True:
try:
s.connect((host,port))
break
except:
print('IP not available')
print('1.Check sender\'s IP')
print('2.Check if sender & receiver are on same network')
host = input('Enter host IP ')
print('Connected to Sender')
tmp = Tk()
tmp.withdraw()
## FILENAME
filename = s.recv(8192).decode()
s.send('ok'.encode())
## FILESIZE
filesize = int(s.recv(1024).decode())
filesizekb = filesize//1024
s.send('ok'.encode())
## LOCATION CHOOSER
f = filedialog.asksaveasfile(initialfile = filename,title = 'Select location to save file',mode='wb')
try:
f.tell()
except AttributeError:
print('Download Cancelled')
return
temp_file = f
data = s.recv(8192)
print('Receiving file..............')
while data:
f.write(data)
data = s.recv(8192)
progress = f.tell()*100//filesize
print(str(progress),'% Downloaded',' {} KB/{} KB'.format(f.tell()//1024,filesizekb),sep='',end='\r')
print(str(progress),'% Downloaded',' {} KB/{} KB'.format(f.tell()//1024,filesizekb),sep = '')
if f.tell()!=filesize:
print('Download failed')
else:
print('file received successfully')
f.close()
s.close()
def main():
try:
receiver()
except KeyboardInterrupt:
print('\nDownload Interrupted')
temp_socket.close()
try:
temp_file.close()
except:
pass
if __name__ == '__main__':
main()