29

I need a python client to do FTPES (explicit), does anyone has experience with any python package that can do this.

I am not able to do this in python, but can connect to FTP server using tools like FileZilla

Thanks

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
user424060
  • 1,545
  • 3
  • 20
  • 29

4 Answers4

32

FTP-SSL Explicit is well supported by native Python. After setting up the connection, you can use all the standard ftplib commands. More can be found at: http://docs.python.org/2/library/ftplib.html#ftplib.FTP_TLS

Here's a basic example for downloading a file:

from ftplib import FTP_TLS
ftps = FTP_TLS('ftp.MySite.com')
# login after securing control channel
ftps.login('testuser', 'testpass')           
# switch to secure data connection.. 
# IMPORTANT! Otherwise, only the user and password is encrypted and
# not all the file data.
ftps.prot_p()          
ftps.retrlines('LIST')

filename = 'remote_filename.bin'
print 'Opening local file ' + filename
with open(filename, 'wb') as myfile:
    ftps.retrbinary('RETR %s' % filename, myfile.write)

ftps.close()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
SilentSteel
  • 2,344
  • 1
  • 28
  • 28
  • 3
    However, for servers requiring reuse of TLS session in data channels, there is a need to subclass FTP_TLS, see workaround here: https://stackoverflow.com/a/43301750. – AndersTornkvist Nov 24 '18 at 09:01
  • 3
    Thanks a lot! I was receiving `ftplib.error_perm: 534 Policy requires SSL` the whole day, solved by using the `.prot_p()` to switch to secure data connection. – Kenny Aires Jun 17 '20 at 21:16
14

For me this worked: (login after auth). Taken from Nullege. They seem to be tests for ftplib.

self.client = ftplib.FTP_TLS(timeout=10)
self.client.connect(self.server.host, self.server.port)

# enable TLS
self.client.auth()
self.client.prot_p()

self.client.login(user,pass)
Nathan Smith
  • 683
  • 1
  • 10
  • 24
StefanMZ
  • 453
  • 1
  • 4
  • 11
1

Standard ftplib does contain everything you need for ftpes (ftps explicit) connection. I didn't find easy way to make implicit connections.

See: http://docs.python.org/2/library/ftplib.html#ftplib.FTP_TLS

Sami Lehtinen
  • 881
  • 10
  • 16
0

I need a python client to do FTPES (explicit), does anyone has experience with any python package that can do this.

ftplib in stdlib should do what you want... an example, lifted from the docs...

>>> from ftplib import FTP_TLS
>>> from getpass import getpass
>>>
>>> ftpes = FTP_TLS('ftp.cisco.com', timeout=5)
>>> passwd = getpass("Enter your password:")
Enter your password:
>>> ftpes.login("username", passwd)   # login before securing channel
>>> ftpes.prot_p()          # switch to secure data connection
>>> ftpes.retrlines('LIST') # list directory content securely
total 9
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 .
drwxr-xr-x   8 root     wheel        1024 Jan  3  1994 ..
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 bin
drwxr-xr-x   2 root     wheel        1024 Jan  3  1994 etc
d-wxrwxr-x   2 ftp      wheel        1024 Sep  5 13:43 incoming
drwxr-xr-x   2 root     wheel        1024 Nov 17  1993 lib
drwxr-xr-x   6 1094     wheel        1024 Sep 13 19:07 pub
drwxr-xr-x   3 root     wheel        1024 Jan  3  1994 usr
-rw-r--r--   1 root     root          312 Aug  1  1994 welcome.msg
'226 Transfer complete.'
>>> filename = "welcome.msg"
>>> ftpes.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
'226 Transfer complete.'
>>> ftpes.close()
>>>
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174