且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-02 08:09:56

解决方案

使用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