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

curl可连接FTPS服务器,但FTP_TLS客户端出现超时故障

显式FTPS连接问题:Python代码卡顿超时,curl可正常连接

我们可以通过以下curl命令正常连接使用显式FTPS的服务器:

curl --ssl --list-only --user USER:PASSWD ftp://ftp.COMPANY.com:port/public

但下面的Python代码在输出“prot_p complete”后会卡顿一段时间,最终返回“Connection timed out”:

import ftplib

with ftplib.FTP_TLS() as ftps:
    print('Connecting')
    ftps.connect(URL, PORT)
    print('Connected')
  
    print('Logging in')
    ftps.login(LOGIN, PASSWD)
    print('Logged in')

    ftps.prot_p()
    print('prot_p complete')

    ftps.retrlines('LIST')

问题分析

curl的--ssl参数会自动为控制连接和数据连接启用SSL/TLS加密,而你的Python代码仅对控制连接启用了TLS(通过prot_p()),但未对数据连接做加密处理。当执行retrlines('LIST')时需要建立数据连接,服务器因数据连接未加密拒绝响应,最终导致超时。

解决方法

在prot_p()之后添加ftps.prot_p(),确保数据连接也使用TLS加密:

修改后的代码:

import ftplib

with ftplib.FTP_TLS() as ftps:
    print('Connecting')
    ftps.connect(URL, PORT)
    print('Connected')
  
    print('Logging in')
    ftps.login(LOGIN, PASSWD)
    print('Logged in')

    ftps.prot_p()  # 加密控制连接
    print('prot_p complete')
    ftps.prot_p()  # 加密数据连接
    print('Data channel protected')

    ftps.retrlines('LIST')

额外排查点

  • 如果仍超时,检查客户端防火墙是否允许服务器的被动模式端口范围(多数FTPS服务器默认使用被动模式)。
  • 可以尝试调整连接超时时间:ftps.connect(URL, PORT, timeout=30)
  • 若服务器要求主动模式,可添加ftps.set_pasv(False)(注意主动模式需要客户端开放端口,可能被防火墙拦截)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 14:57:45