python - 带有 FTPS 的 ftplib storbinary 挂起/从未完成
问题描述
我正在尝试使用 FTPS 将文件上传到 FTP 站点,但是当我尝试存储文件时,它只是在文件完全传输后挂起。
global f_blocksize
global total_size
global size_written
f_blocksize = 1024
total_size = os.path.getsize(file_path)
size_written = 0
file = open(file_path, "rb")
try:
ftps = FTP_TLS("ftp.example.com")
ftps.auth()
ftps.sendcmd("USER username")
ftps.sendcmd("PASS password")
ftps.prot_p()
print(ftps.getwelcome())
try:
print("File transfer started...")
ftps.storbinary("STOR myfile.txt", file, callback=handle, blocksize=f_blocksize)
print("File transfer complete!")
except OSError as ex:
print(ftps.getresp())
except Exception as ex:
print("FTP transfer failed.")
print("%s: %s" %(type(ex), str(ex)))
def handle(block):
global size_written
global total_size
global f_blocksize
size_written = size_written + f_blocksize if size_written + f_blocksize < total_size else total_size
percent_complete = size_written / total_size * 100
print("%s percent complete" %str(percent_complete))
我得到以下输出:
220 Microsoft FTP Service
File transfer started...
3.5648389904264577 percent complete
7.129677980852915 percent complete
10.694516971279374 percent complete
14.25935596170583 percent complete
17.824194952132288 percent complete
21.389033942558747 percent complete
24.953872932985206 percent complete
28.51871192341166 percent complete
32.083550913838124 percent complete
35.648389904264576 percent complete
39.213228894691035 percent complete
42.778067885117494 percent complete
46.342906875543946 percent complete
49.90774586597041 percent complete
53.472584856396864 percent complete
57.03742384682332 percent complete
60.60226283724979 percent complete
64.16710182767625 percent complete
67.7319408181027 percent complete
71.29677980852915 percent complete
74.8616187989556 percent complete
78.42645778938207 percent complete
81.99129677980854 percent complete
85.55613577023499 percent complete
89.12097476066144 percent complete
92.68581375108789 percent complete
96.25065274151436 percent complete
99.81549173194082 percent complete
100.0 percent complete
之后没有进一步的进展,直到连接超时......
FTP transfer failed.
<class 'ftplib.error_temp'>: 425 Data channel timed out due to not meeting the minimum bandwidth requirement.
当程序运行myfile.txt
时,如果我手动连接并查看,我可以在 FTP 站点中看到一个空文件,但是当我取消它或连接超时时,这个空文件就会消失。
在文件完全传输后,我是否需要调用以关闭文件?
解决方案
这似乎是 PythonSSLSocket
类的问题,它在运行时等待来自服务器的数据unwrap
。由于它从未从服务器接收到此数据,因此无法从套接字中解开 SSL 并因此超时。
特别是我在欢迎消息中将这台服务器识别为某个 Microsoft FTP 服务器,它非常适合本博客中所写的问题
“修复”(如果你可以这么称呼的话)是通过编辑和修改方法来阻止SSLSocket
尝试完全解开连接。ftplib.py
FTP_TLS.storbinary()
def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn:
while 1:
buf = fp.read(blocksize)
if not buf: break
conn.sendall(buf)
if callback: callback(buf)
# shutdown ssl layer
if isinstance(conn, ssl.SSLSocket):
# HACK: Instead of attempting unwrap the connection, pass here
pass
return self.voidresp()
推荐阅读
- c# - 将两个带有对象的列表合并到第三个列表中
- php - 如何在 AdminLTE 3 中为 Yii2 框架设置侧边栏宽度
- angular - 在 S3 存储桶中更新文件后,静态网站内容不会立即反映
- python - 抓取 window.location.href 链接
- javascript - 如何将多个表单中的数据输入 MySQL
- javascript - 如何从文件对话框中读取音频文件
- java - 将 TextInputLayout 或任何 UI 组件从 Activity 传递给 DialogFragment
- python - 考虑比例常数以在 Python 中使用 dsolve
- javascript - 使用需要 CSRF 令牌的遗留端点
- java - Android自动点击编程