471,077 Members | 816 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,077 software developers and data experts.

help: loading binary image data into memory


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
4 2485
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
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
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

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.

Similar topics

5 posts views Thread by OneSolution | last post: by
3 posts views Thread by woth | last post: by
3 posts views Thread by Josema | last post: by
10 posts views Thread by eholz1 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.