簡體   English   中英

Python FTP目錄列表掛起

[英]Python FTP Directory Listing Hangs

在Google上進行了大量搜索並查看了許多stackoverflow問題之后,我仍然感到茫然。 我嘗試獲取目錄列表的每個方法都以Python發送兩個命令(TYPE A和PASV)結尾,然后它停止了交談。 有什么建議么?

我的Python(2.7.6)代碼:

from ftplib import FTP

port_num = 21
host_str = "xxxxxx"
ftp = FTP(host_str)
ftp.set_debuglevel(1)
ftp.connect(host_str, port_num)
ftp.getwelcome()
ftp.login("xxxxxx", "xxxxxx")
print "==Getting DIR..."
lines = ftp.retrlines("NLST")
print "==Got DIR..."
print "Returned files list:\n", lines

輸出:

*resp* '220 Microsoft FTP Service'
*welcome* '220 Microsoft FTP Service'
*cmd* 'USER c680-750'
*resp* '331 Password required for c680-750'
*cmd* 'PASS *******'
*resp* '230 User c680-750 logged in'
==Getting DIR...
*cmd* 'TYPE A'
*resp* '200 Command okay.'
*cmd* 'PASV'
*resp* '227 Entering Passive Mode (204,77,162,69,218,190)' Traceback (most recent call last): ...
    raise err error: [Errno 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會說什么:

Status: Resolving address of xxxx
Status: Connecting to xxx.xx.xxx.xx:21...
Status: Connection established, waiting for welcome message...
Response:   220 Microsoft FTP Service
Command:    USER xxxx
Response:   331 Password required for xxxx
Command:    PASS *******
Response:   230 User xxxx logged in
Command:    SYST
Response:   215 UNIX Type: L8
Command:    FEAT
Response:   211-Extensions supported:
Response:    MDTM
Response:    SIZE
Response:   211 END
Status: Server does not support non-ASCII characters.
Status: Connected
Status: Retrieving directory listing...
Command:    PWD
Response:   257 "xxxx" is the current directory
Command:    TYPE I
Response:   200 Command okay.
Command:    PORT 167,186,116,227,234,64
Response:   200 PORT command successful.
Command:    LIST
Response:   150 File status okay; about to open data connection.
Response:   226 Transfer complete, closing data connection.
Status: Directory listing successful

FileZilla使用活動模式(PORT),在該模式下從服務器到客戶端建立數據連接。 相反,您的python代碼使用PASV模式,其中從客戶端到服務器建立了數據連接。 與python一起使用被動模式

ftp.set_pasv(False)

盡管通常首選被動模式,因為如果客戶端位於某個NAT路由器(即家庭中的典型設置)之后,它將起作用,但如果服務器本身位於某個防火牆或NAT設備之后,則它可能會失敗。 在這種情況下,您需要使用主動模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM