Eduardo Flores
Eduardo Flores

Reputation: 1

FTP Timeout error only when using ftplib on a PyInstaller generated executable

I'm trying to make a program that helps upload files to a target device. The program opens the FTP connection as follows: Inside some FTP object

def __keep_alive(self):
    """
    Periodically send NOOP messages. It does nothing but check that the connection is still going
    and keeps it alive.
    """ 
    while self.keep_alive:
      try:
        self.session.voidcmd("NOOP")
        sleep(1)
      except ftplib.all_errors as ex:
          errmsg = 'Error at FTP: (%s)' % ex
          self.logger.debug(errmsg)
          self.keep_alive = False

def open(self):
    """ 
    Opens a new FTP session on top of the current session. 
    
    If there is any, it won't close it for you.
    """
    self.session = ftplib.FTP(self.ip_address, self._staticConfig["ftpUser"], self._staticConfig["ftpPw"])
    self.logger.info(self.session.getwelcome())
    self.session.set_pasv(False)
    self.session.sendcmd('TYPE I')
    self.session_count += 1
    self.keep_alive_thread = threading.Thread(target=self.__keep_alive, daemon=True)
    self.keep_alive = True
    self.keep_alive_thread.start()

When run over the terminal with python /src/ it works well as expected. When i compile it into an executable with PyInstaller i get all sorts of errors after the first upload attempt:

line 102, in fileExistsOnTarget
  File "ftplib.py", line 569, in dir
  File "ftplib.py", line 462, in retrlines
  File "ftplib.py", line 393, in transfercmd
  File "ftplib.py", line 369, in ntransfercmd
ftplib.error_reply: 200 OK

or

line 115, in directoryExistsOnTarget
  File "ftplib.py", line 569, in dir
  File "ftplib.py", line 462, in retrlines
  File "ftplib.py", line 393, in transfercmd
  File "ftplib.py", line 359, in ntransfercmd
  File "ftplib.py", line 281, in sendcmd
  File "ftplib.py", line 254, in getresp
ftplib.error_perm: 550 Permission denied

The upload code looks as follows:

        ftp: ftplib.FTP = self.session

        with open(localpath, 'rb') as f:
            try:
                ftp.sendcmd('TYPE I')
                self.logger.info(ftp.storbinary(f'STOR {remotepath}', fp=f))
                successful = True
                break
            except ftplib.all_errors as ex:
                errmsg = 'Error on FTP: (%s)' % ex
                self.logger.error(errmsg)
                # Reset and try again:
                self.reset_connection()

I have tried modifying PASV/ACTV mode with no luck. Checked the firewall and all permissions are there.

Edit: This is my .spec file for PYInstaller

# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files, collect_submodules


block_cipher = None


a = Analysis(
    ['src\\__main__.py'],
    pathex=[],
    binaries=[],
    datas=[('files\\imgs', 'files\\imgs')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    name='app',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

Upvotes: 0

Views: 29

Answers (0)

Related Questions