2011-02-25 44 views
1

我確信這個問題已經解決,但我似乎無法找到類似的問題使用Windows XP和Python 2.5,我嘗試使用腳本連接到FTP服務器並下載文件。它應該是簡單的,但以下類似的腳本的指示,我得到的錯誤:Python 2.5腳本連接到FTP並下載文件

ftp.login('USERNAME') 
    File "C:\Python25\lib\ftplib.py", line 373, in login 
    if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd) 
    File "C:\Python25\lib\ftplib.py", line 241, in sendcmd 
    return self.getresp() 
    File "C:\Python25\lib\ftplib.py", line 216, in getresp 
    raise error_perm, resp 
error_perm: 530 User USERNAME cannot log in. 

我使用的腳本是:

def handleDownload(block): 
    file.write(block) 
    print ".", 

# Create an instance of the FTP object 
# FTP('hostname', 'username', 'password') 
ftp = FTP('servername') 

print 'ftplib example' 
# Log in to the server 
print 'Logging in.' 
# You can specify username and password here if you like: 
ftp.login('USERNAME', 'password') 
#print ftp.login() 

# This is the directory 
directory = '/GIS/test/data' 
# Change to that directory. 
print 'Changing to ' + directory 
ftp.cwd(directory) 

# Print the contents of the directory 
ftp.retrlines('LIST') 

我很欣賞這可能是一個微不足道的問題,但如果有人可以提供一些見解,這將是非常有益的!

感謝,S

回答

4
ftp.login('USERNAME', 'password') 

與實際數據替換此。根據錯誤,您嘗試以「USERNAME」身份登錄,密碼「password」顯然不起作用。

另外,將ftp = FTP('servername') 中的servername替換爲要連接的服務器的主機名。

-1

第一瑣碎的檢查將是打開一個交互式會話(即自己的FTP到該服務器使用相同的憑證),以確保這不是一個權限問題..

失效的另一個原因,你當連接到MS ftp服務器時,可能需要爲您的用戶名提供域\用戶名

也許這有幫助嗎?

5

我不明白你在使用哪個庫。 Python標準urllib2是足夠了:

import urllib2, shutil 

ftpfile = urllib2.urlopen("ftp://host.example.com/path/to/file") 
localfile = open("/tmp/downloaded", "wb") 
shutil.copyfileobj(ftpfile, localfile) 

如果您需要登錄(匿名登錄是不夠的),然後指定URL裏面的憑據:

urllib2.urlopen("ftp://user:[email protected]/rest/of/the/url")