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:
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.