问题描述
我有一个应该连接到 FTP 的脚本
I have a script that should connect to a FTP
from ftplib import FTP with FTP('IP') as ftp: ftp.login(user='my user', passwd='my password') ftp.cwd('/MY_DIR') ftp.dir()
我有一个错误:ConnectionRefusedError: [Errno 111] 连接被拒绝
ftp 是一个带有 vsftpd 的 EC2
The ftp is an EC2 with vsftpd
pasv_enable=YES pasv_min_port=1024 pasv_max_port=1048 pasv_address=IP pasv_addr_resolve=YES
已经试过了:
代码适用于其他有和没有 TLS 的 FTP(托管在 1and1,OVH...)
Already tried :
The code work on other FTP with and without TLS (hosted on 1and1, OVH...)
我在 NodeJS 中尝试过这个脚本
I tried this script in 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
我已尝试启用 TLS
I have tried enable TLS
ssl_enable=YES require_ssl_reuse=NO
那么sudo service vsftpd 重启
then sudo service vsftpd restart
并使用
FTP_TLS 而不是 FTP但没有工作
and use
FTP_TLS instead of FTP
but did not work
我也尝试通过设置禁用被动模式
Also I tried disable passive mode by setting
pasv_enable=NO
那么sudo service vsftpd restart
和ftp.set_pasv(False)
也没有用
推荐答案
解决方案
使用filezilla调试该方法后,发现尽管我们在/etc/vsftpd.conf中定义了,我们的FTP还是返回0.0.0.0
pasv_adress=IP
这篇文章帮助了我们:https://www.centos.org/论坛/viewtopic.php?t=52408
你必须评论
listen_ipv6=YES
并启用
listen=YES
在/etc/vsftpd.conf
如果您无法访问 FTP 的 vsftpd.conf,您也可以覆盖 ftplib 的类 FTP
Also you can override the ftplib's class FTP if you can't access to vsftpd.conf of the 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
如果发送 '0.0.0.0' 则强制上一个主机
to force the previous host if '0.0.0.0' is send