'Python Ftplib does not upload files

I am creating a program that generates xml and uploads them to an ftp server but I have encountered an error when uploading the file the program freezes for 3-4 minutes and then gives me the error: "[WinError 10054] An existing connection has been forced to be interrupted by the remote host". I have verified that I can access the server and that the user has the necessary permissions to upload files.

class ImplicitFTP_TLS(ftplib.FTP_TLS):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._sock = None

    @property
    def sock(self):
        return self._sock

    @sock.setter
    def sock(self, value):
        if value is not None and not isinstance(value, ssl.SSLSocket):
            value = self.context.wrap_socket(value)
        self._sock = value

myFtps = ImplicitFTP_TLS()
myFtps.connect(host, 990)
myFtps.login(usr,pwd)
myFtps.set_debuglevel(2)
myFtps.prot_p()
myFtps.cwd("./")

    
myFtps.nlst()

try:
    with open("./nameFile.xml", "rb") as contents:
        myFtps.storlines('STOR %s' % namefile, contents)
except Exception as e:
    print(e)

myFtps.close()

Logs:

get* '150 Opening ASCII mode data connection.\n'
*resp* '150 Opening ASCII mode data connection.'
*get* '226 Transfer complete.\n'
*resp* '226 Transfer complete.'
*cmd* 'TYPE I'
*put* 'TYPE I\r\n'
*get* '200 Type set to I.\n'
*resp* '200 Type set to I.'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering Passive Mode ().\n'
*resp* '227 Entering Passive Mode ().'
*cmd* 'STOR namefile'
*put* 'STOR namefile\r\n'
*get* '125 Data connection already open; Transfer starting.\n'
*resp* '125 Data connection already open; Transfer starting.'
[WinError 10054] An existing connection has been forced to be interrupted by the remote host

More details: The file is uploaded but for only as long as you are trying to connect to the server weighing 0kb when the code finishes executing the file is deleted and the logs above appear



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source