【问题标题】:Handshake operation timed out on FTPS file transfer to remoteFTPS 文件传输到远程时握手操作超时
【发布时间】:2019-03-07 13:47:17
【问题描述】:

我在尝试使用 FTP over SSL/TLS 将文件推送到远程 ftp 服务器时遇到一个奇怪的错误。我在网上找不到解决方案的痕迹:/ 请帮忙。

这是我的代码:

from ftplib import FTP_TLS
import sys, os
root = "\\home\\user\\test.txt"
dest = "/Destdir"
ftps = FTP_TLS('xxx.xxx.xxx.xxx')
ftps.set_debuglevel(1)
ftps.set_pasv(False)
ftps.connect(port=21, timeout=80)
ftps.login('user', 'pass')
ftps.prot_p()
ftps.ccc()
try:
    ftps.cwd(dest)
except Exception as e:
    print(e)
try:
    file = open('test.txt', 'rb')
    ftps.storbinary('STOR test.txt', file)
    file.close()
except Exception as e:
    print(e)
ftps.close()

这是脚本的输出:

*resp* '220 nas FTP server ready.'
*cmd* 'AUTH TLS'
*resp* '234 AUTH TLS command successful.'
*cmd* 'USER user'
*resp* '331 Password required for user.'
*cmd* 'PASS **********'
*resp* '230 User user logged in.'
*cmd* 'PBSZ 0'
*resp* '200 PBSZ command successful (PBSZ=0).'
*cmd* 'PROT P'
*resp* '200 Protection level set to Private.'
*cmd* 'CCC'
*resp* '200 Clearing control channel protection.'
*cmd* 'CWD /Destdir'
*resp* '250 CWD command successful.'
*cmd* 'TYPE I'
*resp* '200 Type set to I.'
*cmd* 'PORT 10,10,99,11,220,211'
*resp* '200 PORT command successful.'
*cmd* 'STOR test.txt'
*resp* "150 Opening BINARY mode data connection for 'test.txt'."
_ssl.c:704: The handshake operation timed out

由于连接到远程 FTP 服务器没问题,我想这不是防火墙问题。

注意: 远程 FTP 服务器是 Synology NAS,具有最新的操作系统。

EDIT_0:

被动模式的另一次尝试给出了结果:

*resp* '220 nas FTP server ready.'
*cmd* 'AUTH TLS'
*resp* '234 AUTH TLS command successful.'
*cmd* 'USER user'
*resp* '331 Password required for user.'
*cmd* 'PASS **********'
*resp* '230 User user logged in.'
*cmd* 'PBSZ 0'
*resp* '200 PBSZ command successful (PBSZ=0).'
*cmd* 'PROT P'
*resp* '200 Protection level set to Private.'
*cmd* 'CCC'
*resp* '200 Clearing control channel protection.'
*cmd* 'CWD /Destdir'
*resp* '250 CWD command successful.'
*cmd* 'TYPE I'
*resp* '200 Type set to I.'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (xxx,xxx,xxx,xxx,216,241)'
*cmd* 'STOR test.txt'
*resp* "150 Opening BINARY mode data connection for 'test.txt'."
_ssl.c:704: The handshake operation timed out

我现在也尝试过扩展被动模式,但也没有用:

*resp* '250 CWD command successful.'
*cmd* 'TYPE I'
*resp* '200 Type set to I.'
*cmd* 'EPSV'
*resp* '229 Entering Extended Passive Mode (|||55536|)'
*cmd* 'STOR test.txt'
*resp* "150 Opening BINARY mode data connection for 'test.txt'."
_ssl.c:704: The handshake operation timed out

EDIT_1: 所以脚本部分工作,它能够打开连接,开始传输文件。文件获取是在远程服务器上创建的,但它不包含与源文件相同的数据。目标文件以 1KB 大小和一些随机字符结束(文件是 ANSII 编码的,而源文件是 UTF8. 同时,我可以使用 WinSCP 成功上传文件。

【问题讨论】:

  • 你为什么使用active模式?您尝试过被动吗?
  • 嗨@MartinPrikryl,我没试过。我必须承认这是我第一次使用 FTPS 脚本,所以我完全是 n00b ;)

标签: python ftp ftps


【解决方案1】:

您使用 FTP 主动模式:

ftps.set_pasv(False)

从您的 cmets 看来,您似乎没有充分的理由这样做。

FTP 主动模式通常不起作用,因为它要求客户端能够接受传入的连接。如果不打开本地(Windows 和本地网络)防火墙和/或配置 NAT,这通常是不可能的。甚至不要尝试使用主动模式,除非您有非常具体的理由这样做。使用被动模式。只需删除 ftps.set_pasv 调用即可。被动模式是ftplib中的默认模式。

详情请看我在FTP connection modes上的文章。

【讨论】:

    【解决方案2】:

    很可能是防火墙问题。 FTP 相当特殊,因为它每次传输使用不同的连接。当前根据您的跟踪工作的连接是 command 连接,它由客户端建立并以服务器 FTP 端口(通常为 21)为目标。

    数据连接是一个独立的连接。在主动模式下,它由服务器建立,源自 FTP-DATA 端口 (20),并以客户端在其PORT 命令中发送的端口为目标。在被动模式下,服务器在响应 PASV 命令时发送将侦听数据连接的端口,客户端尝试打开到该(几乎是随机的)端口的新连接。

    这意味着除非防火墙允许较大的地址范围,或者防火墙软件实施特殊处理以动态允许在 FTP 命令中传递的地址,否则防火墙可以允许命令连接,然后阻止任何数据连接。

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多