【发布时间】: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 ;)