By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

help: loading binary image data into memory

P: n/a

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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)

iD8DBQFCWyRhJd01MZaTXX0RArwRAJ9cQqRWBGJB4y9HCSxcWj waJ9cxkgCfZhoI
QDOoVf7HFQwlMIDWasoEPYM=
=xXl5
-----END PGP SIGNATURE-----

Jul 18 '05 #2

P: n/a
Jeff Epler wrote:
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
rk***@ucsd.edu

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

Jul 18 '05 #3

P: n/a
Robert Kern wrote:
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)


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>

Jul 18 '05 #4

P: n/a

tx vm guys, helped a lot

"rixdelei" <ri******@hotmail.com> escribió en el mensaje
news:d3***********@news.wplus.net...

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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.