无法使用ftplib列出FTP目录–但FTP客户端可以工作

2024-09-24 04:18:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图连接到FTP,但我无法运行任何命令。在

ftp_server = ip
ftp_username = username
ftp_password = password

ftp = ftplib.FTP(ftp_server)
ftp.login(ftp_username, ftp_password)
'230 Logged on'

ftp.nlst()

ftp.nlst引发此错误:

Error:
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


我用FileZilla(在同一台机器上运行)测试了连接,它工作得很好。在

这是FileZilla日志:

^{pr2}$

Tags: 命令ipserverusernameftppasswordconnectionrespond
1条回答
网友
1楼 · 发布于 2024-09-24 04:18:14

Status: Server sent passive reply with unroutable address

以上说明FTP服务器配置错误。它将其内部网络IP发送到外部网络(到客户端-FileZilla或Python ftplib),在那里它是无效的。FileZilla可以检测到这一点并自动返回到服务器的原始IP地址。在

pythonftplib不执行这种检测。在

您需要修复FTP服务器以返回正确的IP地址。在


如果无法修复服务器(它不是您的服务器,而且管理员不合作),您可以通过重写FTP.makepasv使ftplib忽略返回的(无效的)IP地址并使用原始地址:

class SmartFTP(FTP):
    def makepasv(self):
        invalidhost, port = super(SmartFTP, self).makepasv()
        return self.host, port

ftp = SmartFTP(ftp_server)

# the rest of the code is the same

相关问题 更多 >