Python 2.5脚本连接到FTP并下载文件 [英] Python 2.5 script to connect to FTP and download file

查看:270
本文介绍了Python 2.5脚本连接到FTP并下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信之前已经解决了这个问题,但我似乎无法找到类似的Q& A(新手)
使用Windows XP和Python 2.5,我尝试使用脚本连接到FTP服务器,下载文件。它应该很简单,但按照类似脚本的说明,我会得到这些错误:

pre $ ftc.login('USERNAME')
文件C:\Python25\lib\ftplib.py,第373行,在登录
如果resp [0] =='3':resp = self.sendcmd('PASS'+ passwd )
文件C:\Python25\lib\ftplib.py,第241行,在sendcmd
中返回self.getresp()
文件C:\Python25\ lib\ftplib.py,第216行,在getresp中
提高error_perm,resp
error_perm:530用户USERNAME无法登录。

我使用的脚本是:

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

#创建一个FTP对象的实例
#FTP('hostname ','username','password')
ftp = FTP('servername')

print'ftplib example'
#登录服务器
print '登录'。
#您可以指定用户名a nd密码在这里如果你喜欢:
ftp.login('USERNAME','password')
#print ftp.login()

#这是目录
directory ='/ GIS / test / data'
#切换到该目录。
print'更改为'+目录
ftp.cwd(目录)

#打印目录中的内容
ftp.retrlines('LIST')

我很欣赏这可能是一个微不足道的问题,但如果任何人都可以提供一些见解,那将非常有帮助! / p>

谢谢,S

解决方案

我无法理解哪个库是你使用。 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:password@host.example.com/rest/of/the/url)


I am sure this has been resolved before but I cannot seem to find a similar Q&A (newbie) Using Windows XP and Python 2.5, I m trying to use a script to connect to an FTP server and dowload files. It should be simple but following the instructions of similar scripts I get the errors:

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.

The script I use is:

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')

I appreciate this might be a trivial question, but if anyone can provide some insights it would be very helpful!

Thanks, S

解决方案

I can't understand which library are you using. Python standard urllib2 is sufficient:

import urllib2, shutil

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

If you need to login (anonymous login isn't sufficient), then specify the credentials inside the url:

urllib2.urlopen("ftp://user:password@host.example.com/rest/of/the/url")

这篇关于Python 2.5脚本连接到FTP并下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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