Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- import urllib
- import os
- def reporthook(blocks_read, block_size, total_size):
- """total_size is reported in bytes.
- block_size is the amount read each time.
- blocks_read is the number of blocks successfully read.
- """
- if not blocks_read:
- print 'Connection opened'
- return
- if total_size < 0:
- # Unknown size
- print 'Read %d blocks (%d bytes)' % (blocks_read, blocks_read *
- block_size)
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement