2011-02-25 63 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")