Owlglass

FTP - retrieve files with Python

FTP file download

 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
#!/usr/bin/python3
#
# script to login with ftp, print the directory contents
# and retrieve the files
#
# example: python3 ftp_python.py 8.8.8.8 'user' 'password'

from ftplib import FTP
import sys
import os

ip = str(sys.argv[1])
name = str(sys.argv[2])
pss = str(sys.argv[3])

print('\n')
print('Connecting to '+ip+' through FTP')
print("+++++++++++++++++++++++++++++++")



with FTP(sys.argv[1]) as ftp:
    ftp.login(user=name,passwd=pss)
    print("The current directory is: "+ ftp.pwd())
    ftp.dir()
    filenames = ftp.nlst()
    os.makedirs('./downloads')
    for filename in filenames:
        filedata = open('./downloads/'+filename,'wb')
        ftp.retrbinary('RETR '+filename, filedata.write)
    ftp.close()

wget

1
2
wget -m ftp://anonymous:anonymous@10.10.10.98 #Donwload all
wget -m --no-passive ftp://anonymous:anonymous@10.10.10.98 #Download all