Is it possible to read FTP files without writing them using Python?

I am trying to read files using Python ftplib without writing them. Something roughly equivalent:

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 it does not work. The only examples in the documents are file entries using the ftp.retrbinary('RETR README', open('README', 'wb').write) format ftp.retrbinary('RETR README', open('README', 'wb').write) . Is it possible to read ftp files without writing in the first place?

+23
python ftp ftplib
Jun 26 2018-12-12T00:
source share
1 answer

Well, you have the answer right in front of you: the retrbinary method takes as a second parameter a link to a function that is called whenever the contents of the file are 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 must implement writeFunc so that it actually adds the read data to the internal variable, something like this that uses the called 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 the Python standard library has a module designed for this kind of thing, StringIO:

 #!/usr/bin/env python from ftplib import FTP from io import StringIO ftp = FTP('ftp.kernel.org') ftp.login() r = StringIO() ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write) print r.getvalue() 

Update 2: StringIO has been added to io. @TimRichardson combined comment:

+44
Jun 26 2018-12-12T00:
source share



All Articles