1

I've been trying to write a python code which can archive the files of ftp server into one zip file and download it :

And so far I understand I need to use ftplib for this:

import os
from time import strftime
from ftplib import FTP

day = strftime("%d")
today = strftime("%d-%m-%Y")

link = FTP(ftphost)
link.login(passwd = ftp_pass, user = ftp_user)
link.cwd(file_path)

And I think I need to use FTP.sendcmd function to send a command to FTP server to have it archive all the files into one and actually I am not really sure which command I need to send though.

To download the file, the function seems legit to me:

import os
def download(ftp,file, localdir):
    f = open(os.path.join(localdir, file),"wb")
    ftp.retrbinary("RETR " + file,f.write)
    f.close()

Can someone put together what I've been trying to do here please?

Tarik
  • 79,711
  • 83
  • 236
  • 349

2 Answers2

1

As far as I know, the FTP protocol does not provide for running arbitrary client-specified commands on the server. See for yourself: RFC 959.

Even if it did, it would be a major security concern (with public FTP servers at least).

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • That's a possibility, provided you have `ssh` access to the server. See http://stackoverflow.com/questions/1233655/what-is-the-simplest-way-to-ssh-using-python – NPE Jan 16 '12 at 22:17
1

OK, so I've found the solution I believe.

I need to use SSH (Secure Shell) or sFTP I think they mean same thing. There is a lib called Paramiko and it offers sending commands, connection, etc. for SSH. It seems using ftplib won't help since you cannot run Shell Commands via ftplib. You need a secure connection to do that.

Tarik
  • 79,711
  • 83
  • 236
  • 349