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:
daniel kullmann Jun 26 2018-12-12T00: 00Z
source share