无法使用ftplib列出FTP目录-但是FTP客户端可以使用 [英] Cannot list FTP directory using ftplib – but FTP client works

查看:192
本文介绍了无法使用ftplib列出FTP目录-但是FTP客户端可以使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到FTP,但是我无法运行任何命令.

I'm trying to connect to an FTP but I am unable to run any commands.

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引发此错误:

错误:
[WinError 10060]连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能响应,建立的连接失败

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(在同一台计算机上运行)测试了连接,并且工作正常.


I've tested the connection using FileZilla (running on the same machine) and it works fine.

这是FileZilla日志:

This is FileZilla log:

Status: Connection established, waiting for welcome message...
Status: Insecure server, it does not support FTP over TLS.
Status: Logged in Status: Retrieving directory listing...
Status: Server sent passive reply with unroutable address. Using server address instead.
Status: Directory listing of "/" successful

推荐答案

状态:服务器发送了无法路由的被动回复

Status: Server sent passive reply with unroutable address

以上表示FTP服务器配置错误.它将内部网络IP发送到无效的外部网络(到客户端– FileZilla或Python ftplib). FileZilla可以检测到该错误并自动回退到服务器的原始IP地址.

The above means that the FTP server is misconfigured. It sends its internal network IP to outside network (to the client – FileZilla or Python ftplib), where it is invalid. FileZilla can detect that and automatically fall back to the original IP address of the server.

Python ftplib不会进行这种检测.

Python ftplib does not do this kind of detection.

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

You need to fix your FTP server to return the correct IP address.

如果修复服务器不可行(这不是您的服务器,并且管理员不合作),则可以使ftplib忽略返回的(无效)IP地址,并使用原始地址,而不是覆盖FTP.makepasv:

If it is not feasible to fix the server (it's not yours and the admin is not cooperative), you can make ftplib ignore the returned (invalid) IP address and use the original address instead by overriding FTP.makepasv:

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


另一种解决方案可能是使用IPv6.参见 Python 3.8.5 FTPS连接 .


Another solution may be to use IPv6. See Python 3.8.5 FTPS connection.

这篇关于无法使用ftplib列出FTP目录-但是FTP客户端可以使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆