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

前端 未结 1 2014
小蘑菇
小蘑菇 2021-01-21 04:58

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(\         


        
相关标签:
1条回答
  • 2021-01-21 05:23

    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

    pasv_adress=IP
    

    this post helped us : https://www.centos.org/forums/viewtopic.php?t=52408

    You have to comment

    listen_ipv6=YES
    

    and enable

    listen=YES
    

    in /etc/vsftpd.conf


    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
    

    to force the previous host if '0.0.0.0' is send

    0 讨论(0)
提交回复
热议问题