简体   繁体   English

Ftplib ConnectionRefusedError: [Errno 111] 连接被拒绝 (python 3.5)

[英]Ftplib ConnectionRefusedError: [Errno 111] Connection refused (python 3.5)

I have a script that should connect to a FTP我有一个应该连接到 FTP 的脚本

from ftplib import FTP

with FTP('IP') as ftp:
   ftp.login(user='my user', passwd='my password')
   ftp.cwd('/MY_DIR')
   ftp.dir()

I have an error : ConnectionRefusedError: [Errno 111] Connection refused我有一个错误: ConnectionRefusedError: [Errno 111] Connection refused


The ftp is an EC2 with vsftpd ftp 是带有 vsftpd 的 EC2

pasv_enable=YES
pasv_min_port=1024
pasv_max_port=1048
pasv_address=IP
pasv_addr_resolve=YES

Already tried :已经尝试过:

The code work on other FTP with and without TLS (hosted on 1and1, OVH...)该代码适用于其他带有和不带有 TLS 的 FTP(托管在 1and1、OVH...)


I tried this script in NodeJS我在 NodeJS 中尝试过这个脚本

const ftpClient = require('ftp-client');

const client = new ftpClient({
   host: "IP",
   port: 21,
   user: "My user", // defaults to "anonymous"
   password: "My password" // defaults to "@anonymous"
});

client.connect(() => {

  client.download('/MY_DIR/file','/tmp/file', (res) => {
        console.log(res)
  })
});

Works perfectly fine so I exclude a firewall problem工作得很好,所以我排除了防火墙问题


I have tried enable TLS我试过启用 TLS

ssl_enable=YES
require_ssl_reuse=NO

then sudo service vsftpd restart然后 sudo 服务 vsftpd 重启

and use并使用
FTP_TLS instead of FTP but did not work FTP_TLS而不是FTP但不起作用


Also I tried disable passive mode by setting我也尝试通过设置禁用被动模式

pasv_enable=NO

then sudo service vsftpd restart然后sudo service vsftpd restart

and ftp.set_pasv(False)ftp.set_pasv(False)

didn't work either也没有用

Solution解决方案

After using filezilla to debug the method, turn out that our FTP returned 0.0.0.0 despite we defined in /etc/vsftpd.conf使用filezilla调试该方法后,尽管我们在/etc/vsftpd.conf定义了我们的FTP返回0.0.0.0

pasv_adress=IP

this post helped us : https://www.centos.org/forums/viewtopic.php?t=52408这篇文章帮助了我们: https : //www.centos.org/forums/viewtopic.php?t=52408

You have to comment你必须评论

listen_ipv6=YES

and enable并启用

listen=YES

in /etc/vsftpd.conf/etc/vsftpd.conf


Also you can override the ftplib's class FTP if you can't access to vsftpd.conf of the FTP如果您无法访问 FTP 的 vsftpd.conf,您也可以覆盖 ftplib 的类 FTP

class CustomFTP(ftplib.FTP):

    def makepasv(self):
        if self.af == socket.AF_INET:
            host, port = ftplib.parse227(self.sendcmd('PASV'))
        else:
            host, port = ftplib.parse229(self.sendcmd('EPSV'), self.sock.getpeername())

        if '0.0.0.0' == host:
            """ this ip will be unroutable, we copy Filezilla and return the host instead """
            host = self.host
        return host, port

to force the previous host if '0.0.0.0' is send如果发送'0.0.0.0'则强制前一个主机

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python ftplib 下载但得到 ConnectionRefusedError: [Errno 111] Connection refused - use python ftplib downloading but get ConnectionRefusedError: [Errno 111] Connection refused ConnectionRefusedError:[Errno 111]连接被拒绝 - ConnectionRefusedError: [Errno 111] Connection refused python 套接字返回 ConnectionRefusedError: [Errno 111] 连接被拒绝 - python socket return ConnectionRefusedError: [Errno 111] Connection refused python-valve rcon minecraft'ConnectionRefusedError:[Errno 111]连接被拒绝' - python-valve rcon minecraft 'ConnectionRefusedError: [Errno 111] Connection refused' Python ConnectionRefusedError:[Errno 111] Docker 容器上的连接被拒绝 - Python ConnectionRefusedError: [Errno 111] Connection refused on Docker container ConnectionRefusedError: [Errno 111] 连接被拒绝- Mlrun - ConnectionRefusedError: [Errno 111] Connection refused- Mlrun Python 3.5 connection.py: sock.connect(sa) ConnectionRefusedError: [Errno 111] - Python 3.5 connection.py: sock.connect(sa) ConnectionRefusedError: [Errno 111] Flask+requests API ConnectionRefusedError: [Errno 111] 连接被拒绝 - Flask+requests API ConnectionRefusedError: [Errno 111] Connection refused 在Python(多处理)中正确酸洗和取消代理对象? “ConnectionRefusedError:[Errno 111] 解压时连接被拒绝” - Properly pickling and unpickling proxy objects in Python (multiprocessing)? "ConnectionRefusedError: [Errno 111] Connection refused while unpickling" ConnectionRefusedError:[Errno 111] 笔记本电脑和树莓派之间使用 python 连接被拒绝 - ConnectionRefusedError: [Errno 111] Connection refused between laptop and raspberry pi using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM