問題描述
我正在嘗試連接到 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] 連接嘗試失敗,因為連接的一方在一段時間后沒有正確響應(yīng),或者連接的主機(jī)沒有響應(yīng),建立連接失敗
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
<小時>
我已經(jīng)使用 FileZilla 測試了連接(在同一臺機(jī)器上運行),它工作正常.
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
推薦答案
狀態(tài):服務(wù)器發(fā)送了帶有不可路由地址的被動回復(fù)
Status: Server sent passive reply with unroutable address
以上表示FTP服務(wù)器配置錯誤.它將其內(nèi)部網(wǎng)絡(luò) IP 發(fā)送到外部網(wǎng)絡(luò)(發(fā)送到客戶端 - FileZilla 或 Python ftplib),在該位置它是無效的.FileZilla 可以檢測到并自動回退到服務(wù)器的原始 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.
您需要修復(fù)您的 FTP 服務(wù)器以返回正確的 IP 地址.
You need to fix your FTP server to return the correct IP address.
如果修復(fù)服務(wù)器不可行(它不是你的并且管理員不合作),你可以通過覆蓋 FTP.makepasv 讓 ftplib 忽略返回的(無效的)IP 地址并使用原始地址
:
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 連接.
對于具有類似后果的不同問題,請參閱 vsftpd 返回 0,0,0,0 以響應(yīng) PASV一個>.
這篇關(guān)于無法使用 ftplib 列出 FTP 目錄 - 但 FTP 客戶端可以工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!