問(wèn)題描述
我有一個(gè)應(yīng)該連接到 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()
我有一個(gè)錯(cuò)誤:ConnectionRefusedError: [Errno 111] 連接被拒絕
ftp 是一個(gè)帶有 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
已經(jīng)試過(guò)了:
代碼適用于其他有和沒(méi)有 TLS 的 FTP(托管在 1and1,OVH...)
Already tried :
The code work on other FTP with and without TLS (hosted on 1and1, OVH...)
我在 NodeJS 中嘗試過(guò)這個(gè)腳本
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)
})
});
工作得很好,所以我排除了防火墻問(wèn)題
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
但沒(méi)有工作
and use
FTP_TLS
instead of FTP
but did not work
我也嘗試通過(guò)設(shè)置禁用被動(dòng)模式
Also I tried disable passive mode by setting
pasv_enable=NO
那么sudo service vsftpd restart
和ftp.set_pasv(False)
也沒(méi)有用
推薦答案
解決方案
使用filezilla調(diào)試該方法后,發(fā)現(xiàn)盡管我們?cè)?code>/etc/vsftpd.conf中定義了,我們的FTP還是返回0.0.0.0
pasv_adress=IP
這篇文章幫助了我們:https://www.centos.org/論壇/viewtopic.php?t=52408
你必須評(píng)論
listen_ipv6=YES
并啟用
listen=YES
在/etc/vsftpd.conf
如果您無(wú)法訪問(wèn) FTP 的 vsftpd.conf,您也可以覆蓋 ftplib 的類(lèi) 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
如果發(fā)送 '0.0.0.0'
則強(qiáng)制上一個(gè)主機(jī)
to force the previous host if '0.0.0.0'
is send
這篇關(guān)于Ftplib ConnectionRefusedError:[Errno 111] 連接被拒絕(python 3.5)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!