help: loading binary image data into memory

Discussion in 'Python' started by rixdelei, Apr 12, 2005.

  1. rixdelei

    rixdelei Guest

    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
    rixdelei, Apr 12, 2005
    #1
    1. Advertising

  2. rixdelei

    Jeff Epler Guest

    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-----
    Jeff Epler, Apr 12, 2005
    #2
    1. Advertising

  3. rixdelei

    Robert Kern Guest

    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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Apr 12, 2005
    #3
  4. 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>
    Fredrik Lundh, Apr 12, 2005
    #4
  5. rixdelei

    rixdelei Guest

    Re: loading binary image data into memory

    tx vm guys, helped a lot

    "rixdelei" <> escribió en el mensaje
    news:d3f6bo$2thb$...
    >
    > 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
    >
    rixdelei, Apr 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Cowald
    Replies:
    2
    Views:
    21,748
    Steve C. Orr [MVP, MCSD]
    Oct 15, 2003
  2. Lucas Cowald

    How to convert JPEG image into a binary data?

    Lucas Cowald, Oct 15, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    308
    Victor Garcia Aprea [MVP]
    Oct 19, 2003
  3. Lucas Cowald

    How to convert JPEG image into a binary data?

    Lucas Cowald, Oct 15, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    166
    Lucas Cowald
    Oct 15, 2003
  4. Lucas Cowald

    How to convert JPEG image into a binary data?

    Lucas Cowald, Oct 15, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    130
    Lucas Cowald
    Oct 15, 2003
  5. edfialk
    Replies:
    0
    Views:
    174
    edfialk
    May 10, 2007
Loading...

Share This Page