-1

I am struggling to download a file using ftplib. I have tried a variety of commands, and my logic seems to see the files that are on the FTP site and can return file sizes, but when I try to download them I get error messages. When I use a program with a GUI like WinSCP or Filezilla, I am able to download the file no problem, but I want to automate the process.

When I run the following, I expect to get the file downloaded to my working directory, but I get an error

from ftplib import FTP_TLS
ftps = FTP_TLS("ftp.xxxxx.com")
ftps.set_debuglevel(2) 
ftps.set_pasv(True)            
ftps.auth()            
ftps.prot_p()
ftps.login(username, password)
with open('Hello.txt','wb') as fp:
    ftps.retrbinary('RETR Hello.txt', fp.write)    # [Errno 11] Resource temporarily unavailable

ftps.nlst() # [Errno 11] Resource temporarily unavailable

ftps.quit()

I tried running the logic on a different computer (thinking it is possible my current company is restricting the FTP connection, and get a different error

SSLEOFError: EOF occurred in violation of protocol (_ssl:c:1125)

This leads me to believe it is something to do with the "Verifying the Host Key or Certificate in Script" but I am not sure if there is anything I can do about it using ftplib.

EDIT: Logs below

*cmd* 'SIZE /.txt'
*put* 'SIZE /.txt\r\n'
*get* '213 12\n'
*resp* '213 12'
*cmd* 'SIZE /.xlsx'
*put* 'SIZE /.xlsx\r\n'
*get* '213 51304\n'
*resp* '213 51304'
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 PBSZ=0\n'
*resp* '200 PBSZ=0'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 Protection level set to P\n'
*resp* '200 Protection level set to P'
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Type set to A\n'
*resp* '200 Type set to A'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode\n'
*resp* '227 Entering Passive Mode '
*cmd* 'NLST'
*put* 'NLST\r\n'
*get* '150 Opening data channel for directory listing of "/"\n'
*resp* '150 Opening data channel for directory listing of "/"'
Traceback (most recent call last):

  Cell In[186], line 27
    ftps.nlst() # [Errno 11] Resource temporarily unavailable

  File ~\AppData\Local\anaconda3\Lib\ftplib.py:553 in nlst
    self.retrlines(cmd, files.append)

  File ~\AppData\Local\anaconda3\Lib\ftplib.py:462 in retrlines
    with self.transfercmd(cmd) as conn, \

  File ~\AppData\Local\anaconda3\Lib\ftplib.py:393 in transfercmd
    return self.ntransfercmd(cmd, rest)[0]

  File ~\AppData\Local\anaconda3\Lib\ftplib.py:795 in ntransfercmd
    conn = self.context.wrap_socket(conn,

  File ~\AppData\Local\anaconda3\Lib\ssl.py:517 in wrap_socket
    return self.sslsocket_class._create(

  File ~\AppData\Local\anaconda3\Lib\ssl.py:1075 in _create
    self.do_handshake()

  File ~\AppData\Local\anaconda3\Lib\ssl.py:1346 in do_handshake
    self._sslobj.do_handshake()

BlockingIOError: [Errno 11] Resource temporarily unavailable

Log from Filezilla:

Status: Resolving address of ftp.alndata.com
Status: Connecting to xxxxxxx...
Status: Connection established, initializing TLS...
Status: TLS connection established, waiting for welcome message...
Status: Logged in
Status: Retrieving directory listing...
Status: Directory listing of "/" successful

EDIT 3:

Logs from WinSCP that I think are relevant are below. I have been successful at getting the necessary files using both WinSCP and Filezilla

2023-08-07 15:16:48.181 227 Entering Passive Mode (104,214,57,236,85,240)
> 2023-08-07 15:16:48.181 MLSD
. 2023-08-07 15:16:48.181 Connecting to 104.214.57.236:22000 ...
. 2023-08-07 15:16:48.471 Session ID reused
. 2023-08-07 15:16:48.471 Using TLSv1.2, cipher TLSv1.2: ECDHE-RSA-AES256-GCM-SHA384, 2048 bit RSA, xxxx-xxx-xxxxx-xxx-SHA384 TLSv1.2 Kx=ECDH     Au=RSA  Enc=AESGCM(256) Mac=AEAD
< 2023-08-07 15:16:48.471 150 Opening data channel for directory listing of "/"
. 2023-08-07 15:16:48.471 TLS connection established
. 2023-08-07 15:16:48.489 type=file;modify=20230805132639;size=51304; ALN_MRI_Export.xlsx
. 2023-08-07 15:16:48.489 type=file;modify=20230806060018;size=51289; ALN_MRI_Export_2023-08-06_42bc69fc.xlsx
. 2023-08-07 15:16:48.489 type=file;modify=20230804150919;size=12; HelloMRI.txt
< 2023-08-07 15:16:48.529 226 Successfully transferred "/"
. 2023-08-07 15:16:48.529 Data connection closed
  • When I take your code for a simple FTP server, that doesn't use `FTP_TLS`, but `FTP` and omit the calls to `.auth()` and `.prot_p()`, the code works (after fixing the indent error). So, it would appear the problem is specific to the use of the `FTP_TLS` class, or your server. You put the error message in there 3 times - when does it actually occur? It seems unlikely that the `with` line triggers it? – Grismar Aug 07 '23 at 01:48
  • "I get error messages" is pretty useless error description. What error messages? The *"Resource temporarily unavailable"*? Are you sure you get it on `open`? I doubt that! (and if you really do, it's not FTP problem) Post ftplib log file and complete stack trace. Are you running WinSCP/FileZilla on the same machine as your Python code? Post their verbose log file too. (are you aware that WinSCP can scripting capability?) – Martin Prikryl Aug 07 '23 at 06:39
  • @Grismar I cannot change to FTP because I get this error "error_perm: 530 TLS required" but that is what I tried first. The error occurs following the ftps.retrbinary component – wallewannabe Aug 07 '23 at 13:17
  • @Martin Prikryl The error messages are commented out in the code. "# [Errno 11] Resource temporarily unavailable" and it happens after the ftps.retrbinary component, apologies for the indentations. It also occurs after the ftps.nlst() component (if run separately) – wallewannabe Aug 07 '23 at 13:19
  • What about the logs? – Martin Prikryl Aug 07 '23 at 14:17
  • Sorry, new here. Do you mean logs as in this? *cmd* 'NLST' *put* 'NLST\r\n' *get* '150 Opening data channel for directory listing of "/"\n' *resp* '150 Opening data channel for directory listing of "/"' – wallewannabe Aug 07 '23 at 14:26
  • Yes, that looks like an ftplib log (add complete log to your question). And I've asked also for WinSCP/FileZilla logs too. – Martin Prikryl Aug 07 '23 at 15:12
  • @MartinPrikryl not sure what you mean by WinSCP / FileZilla logs but I have added the logs from the console – wallewannabe Aug 07 '23 at 15:22
  • @MartinPrikryl I have added logs. Works perfectly in Filezilla. – wallewannabe Aug 07 '23 at 17:51
  • That's not a proper log file. Those are mostly useless GUI messages. + Was the IP address in 227 response, which you have removed, a correct IP address of your server? + Do you have an access to server-side logs? + Try this https://stackoverflow.com/q/14659154/850848 – Martin Prikryl Aug 07 '23 at 19:29
  • @MartinPrikryl I enabled logging on WinSCP and copied in the details. I am not getting the same error you referenced stackoverflow.com/q/14659154/850848 but I tried some of those solutions to no avail. – wallewannabe Aug 07 '23 at 20:47
  • Complete log files (both WinSCP and ftplib) would be more useful. + Please show us the new code that you have tried based on the link. + You didn't answer my question about the IP address (and now that you have posted it in the WinSCP log, there's no need to hide it in the ftplib log anyway). – Martin Prikryl Aug 08 '23 at 09:32
  • @MartinPrikryl Thank you for the help. The owners of the server into which I am trying to remote changed some security settings, and it is now working. Had the security settings remained the same, I am not sure how I would have solved this, but for now this issue is closed. – wallewannabe Aug 08 '23 at 16:05
  • So please delete your question. – Martin Prikryl Aug 09 '23 at 05:32

0 Answers0