【问题标题】:ftplib storbinary with FTPS is hanging/never completing带有 FTPS 的 ftplib storbinary 挂起/从未完成
【发布时间】:2018-05-01 11:15:24
【问题描述】:

我正在尝试使用 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.

当程序运行时,如果我手动连接并查看,我可以在 FTP 站点中看到一个空的myfile.txt,但是当我取消它或连接超时时,这个空文件就会消失。

在文件完全传输后,我需要调用什么来关闭文件吗?

【问题讨论】:

    标签: python ssl ftp ftplib ftps


    【解决方案1】:

    这似乎是 Python 的 SSLSocket 类的问题,它在运行 unwrap 时正在等待来自服务器的数据。由于它从未从服务器接收到此数据,因此无法从套接字中解开 SSL,因此会超时。

    特别是我在欢迎消息中将这台服务器识别为某个 Microsoft FTP 服务器,它非常适合this blog 中所写的问题

    “修复”(如果你可以这么称呼的话)是通过编辑 ftplib.py 并修改 FTP_TLS.storbinary() 方法来阻止 SSLSocket 尝试完全解开连接。

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

    【讨论】:

      【解决方案2】:

      在使用 python 的 ftplib.FTP_TLSprot_p 和 Microsoft FTP 服务器时,我在 STORBINARY 函数上遇到了这个问题。

      例子:

      ftps = FTP_TLS(host,username,password)
      ftps.prot_p
      STORBINARY...
      

      错误指示解包函数超时。

      与以下问题有关:

      https://www.sami-lehtinen.net/blog/python-32-ms-ftps-ssl-tls-lockup-fix

      https://bugs.python.org/issue10808

      https://bugs.python.org/issue34557

      分辨率:

      1. 打开ftplib的python页面:https://docs.python.org/3/library/ftplib.html

      2. 点击源代码,您将看到类似这样的内容:https://github.com/python/cpython/blob/3.10/Lib/ftplib.py

      3. 将此代码的副本创建到您的项目中(例如:my_lib\my_ftplib.py

      4. 对于失败的方法,在您的情况下为 STORBINARY,错误看起来在该方法中显示 conn.unwrap() 的行上。评论这一行。输入关键字pass 否则空的if 块将给出语法错误。

      5. 在您要实例化 FTP_TLS 的文件中导入上述库。现在您将不再面临此错误。

      推理: 函数def ntransfercmd(在FTP_LTS 类下)中的代码将conn 对象包含在一个SSL 会话中。您评论的上述行负责拆除 SSL 会话后传输。出于某种原因,在使用 Microsoft 的 FTP 服务器时,代码在该行被阻塞并导致超时。这可能是因为传输后服务器断开了连接,也可能是服务器从其一侧解开了 SSL。我不确定。评论该行是无害的,因为最终连接无论如何都会关闭 - 详情请参见下文:

      在 ftplib 的 python 代码中,您会注意到 STORBINARY 函数中的 conn 对象包含在 with 块中,并且它是使用 socket.create_connection 创建的。这意味着当代码退出with 块时会自动调用.close()(您可以通过查看python 套接字类源代码上的__exit__ 方法来确认这一点)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 2012-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-12
        • 1970-01-01
        相关资源
        最近更新 更多