There are several answers on stack overflow about retrieving a FTP file and writing it to a stream such as a string buffer or a file which can then be iterated on.
Such as: Read a file in buffer from FTP python
However, these solutions involve loading the entire file into memory or downloading it to the disk before beginning to process the contents.
I do not have enough memory to buffer the whole file and I do not have access to the disk. This can be done by processing the data in the callback function, but I want to know if it's possible to wrap the ftp code in some magic that returns an iterator rather than peppering my code with callbacks.
I.E. rather than:
def get_ftp_data(handle_chunk):
...
ftp.login('uesr', 'password') # authentication required
ftp.retrbinary('RETR etc', handle_chunk)
...
get_ftp_data(do_stuff_to_chunk)
I want:
for chunk in get_ftp_data():
do_stuff_to_chunk(chunk)
And (unlike existing answers) I want to do it without writing the entire ftp file to disk or memory before iterating on it.