help: loading binary image data into memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rixdelei

    #1

    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.Strin gIO(url_file.re ad())
    ....
    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


  • Jeff Epler

    #2
    Re: help: loading binary image data into memory

    probably something like this: (untested)
    def make_ftplib_cal lback(f):
    def callback(block) : f.write(block)
    return callback
    img = cStringIO.Strin gIO()
    retrbinary( "get ???", make_ftplib_cal lback(img))

    Jeff

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

    iD8DBQFCWyRhJd0 1MZaTXX0RArwRAJ 9cQqRWBGJB4y9HC SxcWjwaJ9cxkgCf ZhoI
    QDOoVf7HFQwlMID WasoEPYM=
    =xXl5
    -----END PGP SIGNATURE-----

    Comment

    • Robert Kern

      #3
      Re: help: loading binary image data into memory

      Jeff Epler wrote:[color=blue]
      > probably something like this: (untested)
      > def make_ftplib_cal lback(f):
      > def callback(block) : f.write(block)
      > return callback
      > img = cStringIO.Strin gIO()
      > retrbinary( "get ???", make_ftplib_cal lback(img))[/color]

      Ummm, how about

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

      ?

      --
      Robert Kern
      rkern@ucsd.edu

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

      Comment

      • Fredrik Lundh

        #4
        Re: help: loading binary image data into memory

        Robert Kern wrote:
        [color=blue][color=green]
        >> probably something like this: (untested)
        >> def make_ftplib_cal lback(f):
        >> def callback(block) : f.write(block)
        >> return callback
        >> img = cStringIO.Strin gIO()
        >> retrbinary( "get ???", make_ftplib_cal lback(img))[/color]
        >
        > Ummm, how about
        >
        > img = cStringIO.Strin gIO()
        > retrbinary("get ???", img.write)[/color]

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

        img = cStringIO.Strin gIO()
        def callback(block) :
        sys.stdout.writ e(".")
        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>



        Comment

        • rixdelei

          #5
          Re: loading binary image data into memory


          tx vm guys, helped a lot

          "rixdelei" <rixdelei@hotma il.com> escribió en el mensaje
          news:d3f6bo$2th b$1@news.wplus. net...[color=blue]
          >
          > 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.Strin gIO(url_file.re ad())
          > ...
          > 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
          >[/color]


          Comment

          Working...