-1

I have following code:

import pysftp

user = ...
password = ...
host = ...
port = 21

class FtpDirectory:
    def __init__(self):
        self.ftp = None

    def __enter__(self):
        self.ftp = FTP_TLS()
        self.ftp.debugging = 1
        self.ftp.connect(host, port)
        self.ftp.login(
            user,
            password,
        )
        self.ftp.prot_p()

        self.ftp.cwd(targetdir)
        return self.ftp

files = [('test1.txt', b'test1content'), ('test2.txt', b'test2content')]

with FtpDirectory() as ftp:
    for file in files:
        ftp.storbinary("STOR " + file[0], BytesIO(file[1]))

But it fails to finish the transmission and throws an Exception (ftplib.error_temp: 426 Transfer failed) even though I can see the first file on the ftps server with the right content.

The ftp log is following

*resp* '220 myexampleserver.xyz X2 WS_FTP Server 8.7.2(01051325)'
*cmd* 'AUTH TLS'
*resp* '234 SSL enabled and waiting for negotiation'      
*cmd* 'USER myexampleuser'
*resp* '331 Enter password'   
*cmd* 'PASS ************'
*resp* '230 User logged in'
*cmd* 'PBSZ 0'
*resp* '200 PBSZ=0'
*cmd* 'PROT P'     
*resp* '200 PRIVATE data channel protection level set'
*cmd* 'CWD ftp_test_a'
*resp* '250 Command CWD succeed'
*cmd* 'TYPE I'
*resp* '200 Transfer mode set to BINARY'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (111,222,33,44,156,110).'
*cmd* 'STOR test.txt'
*resp* '150 Uploading in BINARY file test1.txt' <---- TAKES A WHILE,  FILE APPEARS ON SERVER
*cmd* 'QUIT'
*resp* '426 Transfer failed' 

I don´t get, why WinSCP connects and transfers files without problems while this Python Library is not capable of doing the same.

Here are some settings of WinSCP:

enter image description here

enter image description here

urlopen('https://www.howsmyssl.com/a/check', context=ssl._create_unverified_context()).read() gives me "tls_version":"TLS 1.3"

I don´t necessarily have to use ftplib but I didn´t find some working alternative so far with Python. But in any case I must get this done somehow and any help would be really appreciated.

A. L
  • 131
  • 2
  • 12

1 Answers1

0

Since your problem is not a direct duplicate, here are some of the most famous issue-solvers:

  • Thank you. I tried the suggested solutions. set_pasv(False) I had been trying already before. tranfercmd worked for only download, not for upload. There I have the same timeout behaviour. And the last one brings also a timeout at Python\lib\lssl.py at line 1318 in unwrap s= self._sslobj.shutdown(). Apparently this library is not made for production use and I have to implement some workaround. Why can´t it just work out of the box like WinSCP does, this error is at least 5 years old – A. L Mar 08 '23 at 18:01
  • @A.L All that information should be in your question, not buried here in a comment. – Martin Prikryl Mar 09 '23 at 06:35