Python FTPS 上传错误:425 无法建立数据连接:不允许操作 [英] Python FTPS upload error: 425 Unable to build data connection: Operation not permitted

查看:62
本文介绍了Python FTPS 上传错误:425 无法建立数据连接:不允许操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ftps 将文件发送到 FTP 服务器.登录和更改目录工作:

I'm trying to use ftps to send a file to a FTP server. Login and changing directory work:

import ftplib
ftps = ftplib.FTP_TLS('host','user','pwd')
ftps.set_pasv(True)
ftps.prot_p()
ftps.cwd('/target_directory')

但是当我尝试上传我的文件时:

however when I try to upload my file:

file = open(file, 'rb')
send_cmd = 'STOR file_name.txt'
ftps.storbinary(send_cmd, file)
file.close()
ftps.quit()

我收到以下错误:

File "/script/location/script.py", line 161, in <module>
ftps.storbinary(send_cmd,file)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 772, in storbinary
return self.voidresp()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 229, in voidresp
resp = self.getresp()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py", line 222, in getresp
raise error_temp, resp
ftplib.error_temp: 425 Unable to build data connection: Operation not permitted

我了解到 425 响应代码通常是处于活动模式的结果,这就是我包含 ftps.set_pasv(True)(尽管默认情况下为 True)的原因.

I've read that the 425 response code is often a result of being in active mode, which is why I included ftps.set_pasv(True) (although this is True by default).

我也试过只使用 ftps.retrlines('LIST') 列出目录内容,但得到基本相同的错误.我正在使用 Python 2.7.10.任何帮助将不胜感激.

I've also tried just listing the directory contents using ftps.retrlines('LIST') but get essentially the same error. I'm using Python 2.7.10. Any help would be greatly appreciated.

推荐答案

这是一个在 python 中报告的错误:https://bugs.python.org/issue19500

This is a reported bug in python: https://bugs.python.org/issue19500

您可以在新类中应用补丁

You can apply the patch in a new class

class Explicit_FTP_TLS(ftplib.FTP_TLS):
    """Explicit FTPS, with shared TLS session"""
    def ntransfercmd(self, cmd, rest=None):
        conn, size = ftplib.FTP.ntransfercmd(self, cmd, rest)
        if self._prot_p:
            conn = self.context.wrap_socket(conn,
                                            server_hostname=self.host,
                                            session=self.sock.session)
        return conn, size

这篇关于Python FTPS 上传错误:425 无法建立数据连接:不允许操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆