Tuesday, January 10, 2012

Using FTP/TLS with Python 2.6...

FTP over SSL/TLS can be supported on Python 2.6. If you have prod machines that are running Python 2.6 and you want to avoid upgrading, you can do the following:

wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz

...untar the file and grab the Lib/ftplib.py file.

You can then do:
from ftplib import FTP_TLS

ftps = FTP_TLS('ftp.mysite.com')
ftps.login('userid', 'pw')
ftps.prot_p()
ftps.retrlines('LIST')

To download a file, you would do:
ftps.retrbinary('RETR file.bin', open('output.bin', 'wb').write)

or to upload a file, you would do something on the order of (borrowed from http://www.daniweb.com/software-development/python/threads/110195)
def upload(ftp, file):
  ext = os.path.splitext(file)[1]
  if ext in (".txt", ".htm", ".html"):
     ftp.storlines("STOR %s" % (file), open(file))
  else:
     print ftp.storbinary("STOR %s" % (file), open(file, "rb"), 1024)

3 comments:

  1. I have written a program similar to yours but I am not getting the file copied,
    def upload(ftp, file):
    ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
    ftp.storlines("STOR %s" % (file), open(file))
    else:
    print ftp.storbinary("STOR %s" % (file), open(file, "rb"), 1024)

    upload(ftp, "VOSS7K.0.0.0.0int691.tgz")

    when i login to the switch and search for it shows:
    VSP7254-1:1#ls VOSS7K.0.0.0.0int691.tgz
    -rw-r--r-- 1 0 0 0 Aug 8 01:14 VOSS7K.0.0.0.0int691.tgz

    ReplyDelete