help: loading binary image data into memory

R

rixdelei

Hi there!
Tried successfully downloading data into memory from internet using the
urllib module like this:
....
import urllib
import cStringIO

url_file = urllib.urlopen(url)
img = cStringIO.StringIO(url_file.read())
....
Was wondering HOW could I accomplish the same results using the ftplib
module
-> retrbinary( command, callback[, maxblocksize[, rest]])
WITHOUT saving any information to the disk;

Will appreciate your comments,
TIA+BRGDS
 
J

Jeff Epler

probably something like this: (untested)
def make_ftplib_callback(f):
def callback(block): f.write(block)
return callback
img = cStringIO.StringIO()
retrbinary( "get ???", make_ftplib_callback(img))

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCWyRhJd01MZaTXX0RArwRAJ9cQqRWBGJB4y9HCSxcWjwaJ9cxkgCfZhoI
QDOoVf7HFQwlMIDWasoEPYM=
=xXl5
-----END PGP SIGNATURE-----
 
R

Robert Kern

Jeff said:
probably something like this: (untested)
def make_ftplib_callback(f):
def callback(block): f.write(block)
return callback
img = cStringIO.StringIO()
retrbinary( "get ???", make_ftplib_callback(img))

Ummm, how about

img = cStringIO.StringIO()
retrbinary("get ???", img.write)

?

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
F

Fredrik Lundh

Robert said:
Ummm, how about

img = cStringIO.StringIO()
retrbinary("get ???", img.write)

if you want some additional code to run during download, you can
always do something like:

img = cStringIO.StringIO()
def callback(block):
sys.stdout.write(".")
img.write(block)
if img.tell() > LIMIT:
raise IOError("too much data")
retrbinary(..., callback())

(you don't really have to create a factory if all you need is a single
function...)

you can also get rid of the cStringIO module :

img = []
retrbinary(..., img.append)
# img is now a list of string chunks

might be more efficient, depending on what you plan to do with the
data once you've loaded it.

and finally,

img = urllib.urlopen("ftp://...").read()

might also work.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top