0

I'm trying to fetch files from an FTP Server using implicit FTP over TLS. This works fine in Filezilla.

However, following code in Python 3.9.5

from ftplib import FTP_TLS
import ssl
import sys, os

ftps = FTP_TLS('xxx')
ftps.debugging = 5
ftps.login('yyy', 'zzz')
ftps.prot_p()

filenames = ftps.nlst()
for filename in filenames:
    local_filename = os.path.join('./output/', filename)
    file = open(local_filename, 'wb')
    ftps.retrbinary('RETR '+ filename, file.write)
    file.close()

ftps.quit()

Gives me the following Traceback

Traceback (most recent call last):
  File "/home/epieters/repos/xxxx/yyyy.py", line 10, in <module>
    filenames = ftps.nlst()
  File "/home/epieters/.pyenv/versions/3.9.5/lib/python3.9/ftplib.py", line 553, in nlst
    self.retrlines(cmd, files.append)
  File "/home/epieters/.pyenv/versions/3.9.5/lib/python3.9/ftplib.py", line 479, in retrlines
    conn.unwrap()
  File "/home/epieters/.pyenv/versions/3.9.5/lib/python3.9/ssl.py", line 1285, in unwrap
    s = self._sslobj.shutdown()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2756)

I really have no clue and tried multiple solutions suggested here on stackoverflow. Even when a call a simple ftps.retrlines('LIST') after ftps.prot_p(), I get the same SSL error.

Anybody out there that knows what I'm doing wrong?

epieters
  • 1,077
  • 1
  • 7
  • 10
  • do a pip install pyopenssl and see if errors change. also refer to this for some possible fixes https://stackoverflow.com/a/50681396/11616708 – smcrowley Jul 15 '22 at 20:39
  • In short: ftplib has no builtin support for implicit FTP over TLS (which isn't even standardized). Thus one has to build a solution on top of ftplib and the various linked q+a how how. Apart from that - your code does not actually try to use implicit FTP over TLS, but it uses explicit TLS – Steffen Ullrich Jul 15 '22 at 23:13
  • @smcrowley I was already on the latest pyopenssl, so that didn't help. – epieters Jul 17 '22 at 18:35

0 Answers0