是否有可能读取FTP文件而无需使用Python编写它们? [英] Is it possible to read FTP files without writing them using Python?
问题描述
我正在尝试使用Python的ftplib读取文件而无需编写它们。大致相当于:
def get_page(url):
try:
return urllib.urlopen( url).read()
除外:
return
FTP。
我试过:
def get_page(path):
尝试:
ftp = FTP('ftp.site.com','anonymous','passwd')
return ftp.retrbinary('RETR'+ path,open('page' ).read())
除了:
return''
但是这个不起作用。文档中的唯一示例涉及使用 ftp.retrbinary('RETR README',open('README','wb')。write)
格式编写文件。是否有可能在不先写入的情况下读取ftp文件?
那么,你有正确的答案:方法接受作为第二个参数的引用,该函数在从ftp连接检索文件内容时被调用。
下面是一个简单的例子:
#!/ usr / bin / env python
from ftplib import FTP
def writeFunc(s):
printRead:+ s
ftp = FTP(' ftp.kernel.org')
ftp.login()
ftp.retrbinary('RETR / pub / README_ABOUT_BZ2_FILES',writeFunc)
您应该实现writeFunc,以便它实际将读取的数据附加到内部变量,如下所示,它使用可调用对象:
#!/ usr / bin / env python $ b $ from ftplib import FTP
$ b class读者:
def __init __(self ):
self.data =
def __call __(self,s):
self.data + = s
ftp = FTP('ftp.kernel ('RETR / pub / README_ABOUT_BZ2_FILES',r)
$ b print r。数据
更新:我意识到Python标准中有一个模块图书馆是mea nt用于这类事情,StringIO:
$ b
#!/ usr / bin / env python $ b $ ft from ftplib import FTP
from StringIO import StringIO
ftp = FTP('ftp.kernel.org')
ftp.login()
r = StringIO()
ftp .retrbinary('RETR / pub / README_ABOUT_BZ2_FILES',r.write)
print r.getvalue()
I am trying to read files using Python's ftplib without writing them. Something roughly equivalent to:
def get_page(url):
try:
return urllib.urlopen(url).read()
except:
return ""
but using FTP.
I tried:
def get_page(path):
try:
ftp = FTP('ftp.site.com', 'anonymous', 'passwd')
return ftp.retrbinary('RETR '+path, open('page').read())
except:
return ''
but this doesn't work. The only examples in the docs involve writing files using the ftp.retrbinary('RETR README', open('README', 'wb').write)
format. Is it possible to read ftp files without writing first?
Well, you have the answer right in front of you: The retrbinary method accepts as second parameter a reference to a function that is called whenever file content is retrieved from the ftp connection.
Here is a simple example:
#!/usr/bin/env python
from ftplib import FTP
def writeFunc(s):
print "Read: " + s
ftp = FTP('ftp.kernel.org')
ftp.login()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', writeFunc)
You should implement writeFunc so that it actually appends the data read to an internal variable, something like this, which uses a callable object:
#!/usr/bin/env python
from ftplib import FTP
class Reader:
def __init__(self):
self.data = ""
def __call__(self,s):
self.data += s
ftp = FTP('ftp.kernel.org')
ftp.login()
r = Reader()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r)
print r.data
Update: I realized that there is a module in the Python standard library that is meant for this kind of things, StringIO:
#!/usr/bin/env python
from ftplib import FTP
from StringIO import StringIO
ftp = FTP('ftp.kernel.org')
ftp.login()
r = StringIO()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write)
print r.getvalue()
这篇关于是否有可能读取FTP文件而无需使用Python编写它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!