4

I'm trying to run a simple ftps script to upload a file on a scheduled basis from a Linux box to a ftps instance running on Windows Server 2012. When I try and test the script on my desktop (OS x), the script errors out:

Error uploading file: [Errno 54] Connection reset by peer

If I run the script on a linux box, same error, except 104 instead of 54:

Error uploading file: [Errno 104] Connection reset by peer

The files that I'm uploading have either been empty or are 8 bytes. I've verified that ftps is working with 2 other clients on my desktop. What am I missing / overlooking?

#!/usr/bin/env python
from ftplib import FTP_TLS
import fnmatch
import os
import ssl
import sys

server = '192.168.1.2'
user = 'myUsername'
passwd = 'myPassword'

def connect_ftp():
    ftp = FTP_TLS(server, user, passwd)
    ftp.set_pasv(True)
    ftp.prot_p()

    return ftp

def upload_file(ftp_connection, upload_file_path):
    try:
        upload_file = open("/tmp/test/" + upload_file_path, 'r')
        print('Uploading ' + upload_file_path + "...")
        ftp_connection.storbinary('STOR ' + upload_file_path, upload_file)
        ftp_connection.quit()
        ftp_connection.close()
        upload_file.close()
        print('Upload finished.')

    except Exception, e:
        print("Error uploading file: " + str(e))

ftp_conn = connect_ftp()


for file in os.listdir('/tmp/test'):
        if fnmatch.fnmatch(file, 'bt_*.txt'):
            upload_file(ftp_conn, file)
Cesar
  • 423
  • 5
  • 14

2 Answers2

3

I think this problem only shows on MS FTP server. First of all turn on debug

ftp.set_debuglevel(2)

In my case transfer hangs on

put 'STOR test.xml\r\n'

get '125 Data connection already open; Transfer starting.\n'

resp '125 Data connection already open; Transfer starting.'

Then I found this suggession http://www.sami-lehtinen.net/blog/python-32-ms-ftps-ssl-tls-lockup-fix I've tried (commented conn.unwrap() in storbinary) it and it worked! In my case in was line 513

        # shutdown ssl layer
        if _SSLSocket is not None and isinstance(conn, _SSLSocket):
            pass #conn.unwrap()

This is obliviously very bad hack, but I couldn't find anything better.

Community
  • 1
  • 1
ivan133
  • 817
  • 1
  • 11
  • 22
  • Six years later and I still ran into this issue when uploading files to a FileZilla FTP server v1.0.1 using python 3.9.7... script was hanging at conn.unwrap() and commenting that out worked! Of course the FTP server now complains that the client didn't close the connection properly. But thats ok. However, the solution works. I just wish I knew exactly why the unwarp() function hangs. – Renato Oct 11 '21 at 21:08
  • Seven years later and all quiet on the western front with Python 3.11.0rc2. This little hack still works. – Eakkapat Pattarathamrong Sep 17 '22 at 11:26
-1

I had the same issue and succeeded to solve it with this lines :

ftps = FTP_TLS(server)
ftps.set_debuglevel(2) # To show logs
ftps.ssl_version = ssl.PROTOCOL_TLS
ftps.set_pasv(True)
ftps.login(user="user", passwd="passwd")

And I switched from Python 3.5.1 to Python 3.8.3.

F Blanchet
  • 1,430
  • 3
  • 21
  • 32