Getting the bytes or percent uploaded/downloaded through FTP?

Discussion in 'Python' started by Nainto, Sep 5, 2005.

  1. Nainto

    Nainto Guest

    Hi, I'm just wondering if there is any way to get the number of bytes,
    or the percentage, that have been uploaded/downloaded when
    uploading/downloading a file throught ftp in Python. I have searched
    Google many times but could find nothing.
     
    Nainto, Sep 5, 2005
    #1
    1. Advertisements

  2. Nainto

    Benji York Guest

    If you're using urllib you can do something like this:

    def reporter(block, block_size, total_size):
    left = total_size - block * block_size
    sys.stderr.write('\r')
    sys.stderr.write('Downloading %s: ' % file_name)
    if left > 0: # the estimate is a bit rough, so we fake it a little
    sys.stderr.write('%sK left.' % (left/1024))
    else:
    sys.stderr.write('done.')

    # it's possible that this line is shorter than the previous,
    # so we need to "erase" any leftovers
    sys.stderr.write(' '*10)
    sys.stderr.write('\b'*10)

    import urllib
    urllib.urlretrieve(url, destination, reporter)
    sys.stderr.write('\n')
     
    Benji York, Sep 5, 2005
    #2
    1. Advertisements

  3. Nainto

    Nainto Guest

    Thanks, Benji but I need to be able to get the bytes and/or percent of
    a download too. :-(
     
    Nainto, Sep 5, 2005
    #3
  4. Nainto

    Steve Holden Guest

    The only way I can think of to get the size of the file you're about to
    download is using the FTP object's .dir() method. If you have the Tools
    directory (standard on Windows, with source on other platforms) you can
    take a look at the ftpmirror script - a fairly recent version can be seen at

    http://weblog.cs.uiowa.edu/python-2.3.1/Tools/scripts/ftpmirror.py

    regards
    Steve
     
    Steve Holden, Sep 5, 2005
    #4
  5. to handle arbitrary servers, you also need a flexibel LIST response
    parser; see:

    http://cr.yp.to/ftpparse.html

    python bindings:

    http://c0re.23.nu/c0de/ftpparsemodule/

    and

    http://effbot.org/downloads/#ftpparse

    </F>
     
    Fredrik Lundh, Sep 5, 2005
    #5
  6. Nainto

    Nainto Guest

    Thanks guys. I'll try to these things too work.
     
    Nainto, Sep 5, 2005
    #6
  7. Nainto

    Nainto Guest

    Thanks guys. I'll try to these things too work.
     
    Nainto, Sep 5, 2005
    #7
  8. Nainto

    Nainto Guest

    Wait, can this be used when uploading a file?
     
    Nainto, Sep 5, 2005
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.