[code.view]

[top] / python / PyMOTW / urllib / urllib_urlretrieve.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2008 Doug Hellmann All rights reserved.
     #
     """
     """
     #end_pymotw_header
     
     import urllib
     import os
     
     def reporthook(blocks_read, block_size, total_size):
         if not blocks_read:
             print 'Connection opened'
             return
         if total_size < 0:
             # Unknown size
             print 'Read %d blocks' % blocks_read
         else:
             amount_read = blocks_read * block_size
             print 'Read %d blocks, or %d/%d' % (blocks_read, amount_read, total_size)
         return
     
     try:
         filename, msg = urllib.urlretrieve('http://blog.doughellmann.com/', reporthook=reporthook)
         print
         print 'File:', filename
         print 'Headers:'
         print msg
         print 'File exists before cleanup:', os.path.exists(filename)
     
     finally:
         urllib.urlcleanup()
     
         print 'File still exists:', os.path.exists(filename)

[top] / python / PyMOTW / urllib / urllib_urlretrieve.py

contact | logmethods.com