forked from brandon-rhodes/fopnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advbinarydl.py
executable file
·43 lines (35 loc) · 1.04 KB
/
advbinarydl.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
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter17/advbinarydl.py
import os, sys
from ftplib import FTP
def main():
if os.path.exists('linux-1.0.tar.gz'):
raise IOError('refusing to overwrite your linux-1.0.tar.gz file')
ftp = FTP('ftp.kernel.org')
ftp.login()
ftp.cwd('/pub/linux/kernel/v1.0')
ftp.voidcmd("TYPE I")
socket, size = ftp.ntransfercmd("RETR linux-1.0.tar.gz")
nbytes = 0
f = open('linux-1.0.tar.gz', 'wb')
while True:
data = socket.recv(2048)
if not data:
break
f.write(data)
nbytes += len(data)
print("\rReceived", nbytes, end=' ')
if size:
print("of %d total bytes (%.1f%%)"
% (size, 100 * nbytes / float(size)), end=' ')
else:
print("bytes", end=' ')
sys.stdout.flush()
print()
f.close()
socket.close()
ftp.voidresp()
ftp.quit()
if __name__ == '__main__':
main()