Python Forum
connecting to FTPS server help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
connecting to FTPS server help
#1
First post here so I hope I've put everything I need. Thanks in advance!

I'm new to Python and trying to get my head around some basics.
I have a requirement to be able to connect to an FTPS (implicit on port 990) with username and password.
I've read quite a few snippets of code, but cannot get anything to work with FTPS (standard FTP is OK though).
I'm testing this on a Ubuntu 22.04 LTS server with vsFTPd v3.0.5. My python is 3.10.6.

vsFTPd is running and listening on port 990, TLS is v1.2. It has a valid certificate for the TLS connection and I can connect with a normal FTP client (tried Windows FlashFXP and Ubuntu lftp) successfully.

What I have so far is tried this code:
from ftplib import FTP_TLS
import ssl
def connect():
  ftp = FTP_TLS()
  ftp.ssl_version = ssl.PROTOCOL_TLSv1_2
  ftp.auth()
  ftp.prot_p()
  ftp.connect(host='testsvr3', port=990)
  ftp.login(user='ftpuser1', passwd='testpass')
  return ftp

ftps = connect()
As soon as I try to run that, I get the following:
$ python3 ftp.py
Traceback (most recent call last):
  File "/home/user/ftp.py", line 12, in <module>
    ftps = connect()
  File "/home/user/ftp.py", line 6, in connect
    ftp.auth()
  File "/usr/lib/python3.10/ftplib.py", line 753, in auth
    resp = self.voidcmd('AUTH TLS')
  File "/usr/lib/python3.10/ftplib.py", line 285, in voidcmd
    self.putcmd(cmd)
  File "/usr/lib/python3.10/ftplib.py", line 207, in putcmd
    self.putline(line)
  File "/usr/lib/python3.10/ftplib.py", line 202, in putline
    self.sock.sendall(line.encode(self.encoding))
AttributeError: 'NoneType' object has no attribute 'sendall'
Any ideas where to start looking?
Thanks in advance!
Reply
#2
I haven't watched this video, so can't vouch for it, but looks reasonable: https://www.youtube.com/watch?v=RRHDZTOaKmQ
tfboy likes this post
Reply
#3
(Feb-22-2023, 08:49 PM)Larz60+ Wrote: I haven't watched this video, so can't vouch for it, but looks reasonable: https://www.youtube.com/watch?v=RRHDZTOaKmQ

Thanks Larz. That did help a bit - at least it allowed me to confirm that the problem was not just with the code.

The connect code part includes a timeout option which, when I reduced to 5 seconds, realised the connection was timing out. Upon further investigation, I realised that the reason it was timing out was because it wasn't fully establishing a TLS connection.
And that's because by default, python's built-in ftp_tls.py libraries don't manage implicit FTPS connections which is what I need.

With some further trial and error and research, I found someone else had a similar issue so I took that idea and defined an ImplicitFTP_TLS class in my code so as to not modify the source so updates don't wipe them out. It's detailed here: https://discussions.citrix.com/topic/413...om-python/

import ftplib
import ssl

class ImplicitFTP_TLS(ftplib.FTP_TLS):
    """FTP_TLS subclass that automatically wraps sockets in SSL to support implicit FTPS."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._sock = None

    @property
    def sock(self):
        """Return the socket."""
        return self._sock

    @sock.setter
    def sock(self, value):
        """When modifying the socket, ensure that it is ssl wrapped."""
        if value is not None and not isinstance(value, ssl.SSLSocket):
            value = self.context.wrap_socket(value)
        self._sock = value

from ftplib import all_errors

ftp = ImplicitFTP_TLS()
ftp.set_pasv(True)
ftp.connect(host='testftp1', port=990, timeout=5, source_address=None)
ftp.login('ftpuser1', 'ftppassword')
ftp.prot_p()
try:
    items = []
    print ('get list..')
    ftp.retrlines('LIST', items.append)
    items = map(str.split, items)
    for item in items:
        print (item)
except all_errors as ex:
    print (ex)
finally:
    ftp.quit()
    print ('ftp closed.')
I now have a new issue which is an SSL reuse problem: "522 ssl connection failed session reuse required".
It seems quite well documented and pops up with vsFTPd. I've temporarily for testing worked around that by disabling the feature in vsFTPd but I don't think that's a long-term solution as often, my python script will by used to connect to third party vsFTPd servers where this option may well not have been added.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to take the tar backup files form remote server to local server sivareddy 0 1,917 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Connection timed out error when connecting to SQL server kenwatts275 2 3,348 Jun-02-2020, 07:35 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020