带有 FTPS 的 ftplib storbinary 正在 hanging/never 完成

ftplib storbinary with FTPS is hanging/never completing

我正在尝试使用 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 的 SSLSocket class 的问题,它在 运行 unwrap 时正在等待来自服务器的数据。由于它从未从服务器接收到此数据,因此无法从套接字解包 SSL,因此超时。

我在欢迎消息中特别指出此服务器是某些 Microsoft FTP 服务器,这与 this blog

中所写的问题非常吻合

"fix"(如果你可以这样称呼它的话)是为了阻止 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()

我在使用 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 class 下)将 conn 对象包含在 SSL 会话中。您评论的上述行负责拆除 SSL 会话 post 传输。出于某种原因,当使用 Microsoft 的 FTP 服务器时,代码会在该行被阻塞并导致超时。这可能是因为 post 传输服务器断开了连接,也可能是服务器从其一侧解开了 SSL。我不知道。评论该行是无害的,因为最终无论如何连接都会被关闭——详情见下文:

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