【问题标题】:python ftplib and TLS, issues with data connectionpython ftplib和TLS,数据连接问题
【发布时间】:2019-03-27 10:21:17
【问题描述】:

我需要连接到仅接受 TLS 连接的(可能配置错误的)FTP 服务器。

我正在使用来自 ftplib 的 FTP_TLS。我可以连接、验证和列出文件。 当我尝试上传文件时,出现 534 错误。

我在使用 lftp 时得到了完全相同的行为,但在 lftp 中我可以使用这个标志解决这个问题:

set ftp:ssl-protect-data true

在 python 中,我试过 FTP_TLS.prot_p() 但没有运气。

有什么建议吗?

【问题讨论】:

    标签: python python-3.x ssl ftplib


    【解决方案1】:

    我找到了问题的原因:服务器是 IIS 服务器,似乎 ftplib 与 ISS 存在一些问题 - 或者可能是 IIS 有一些 ftplib 无法处理的“特殊需求”。

    我必须继承 FTP_TLS 类并覆盖 storbinary 方法:

    class iisFTP_TLS(ftplib.FTP_TLS):
        ssl_version=ssl.PROTOCOL_TLS
        def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
            """Store a file in binary mode.  A new port is created for you.
    
            Args:
            cmd: A STOR command.
            fp: A file-like object with a read(num_bytes) method.
            blocksize: The maximum data size to read from fp and send over
                the connection at once.  [default: 8192]
            callback: An optional single parameter callable that is called on
                each block of data after it is sent.  [default: None]
            rest: Passed to transfercmd().  [default: None]
    
            Returns:
            The response code.
            """
            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 _SSLSocket is not None and isinstance(conn, _SSLSocket):
                #   conn.unwrap()
            return self.voidresp()
    
    

    【讨论】:

    • 把 unwrap 换成 close 怎么样?
    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2020-04-21
    • 2014-12-30
    • 2020-09-29
    • 1970-01-01
    相关资源
    最近更新 更多