You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包可执行文件后用ftplib出现FTP超时及权限错误

PyInstaller打包后FTP连接异常解决方案求助

我开发了一款协助文件上传至目标设备的程序,终端运行Python源码时一切正常,但用PyInstaller编译为可执行文件后,首次上传尝试后会出现各类FTP错误,现寻求解决思路。

FTP核心连接逻辑

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()

打包后出现的错误

  • 错误示例1:
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
  • 错误示例2:
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

文件上传代码

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()

已尝试的排查操作

  • 切换FTP的PASV/主动模式,问题依旧
  • 检查本地与目标设备的防火墙配置,确认无连接拦截
  • 验证FTP账号权限配置,确认对目标路径有读写权限

PyInstaller配置文件(.spec)

# -*- 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,
)

内容的提问来源于stack exchange,提问作者Eduardo Flores

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 16:30:01