Created
May 15, 2010 00:24
-
-
Save mikitebeka/401865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| from urllib2 import urlopen | |
| class TooBig(Exception): pass | |
| def download(url, max_size): | |
| fo = urlopen(url) | |
| size = int(fo.headers.get("Content-Length", 0)) | |
| if size and (size > max_size): | |
| raise TooBig() | |
| size = 0 | |
| chunk_size = 1024 | |
| data = [] | |
| while 1: | |
| chunk = fo.read(chunk_size) | |
| if not chunk: | |
| break | |
| data.append(chunk) | |
| size += len(chunk) | |
| if size > max_size: | |
| raise TooBig() | |
| return "".join(data) | |
| def main(argv=None): | |
| import sys | |
| from optparse import OptionParser | |
| argv = argv or sys.argv | |
| parser = OptionParser("%prog URL") | |
| opts, args = parser.parse_args(argv[1:]) | |
| if len(args) != 1: | |
| parser.error("wrong number of arguments") # Will exit | |
| print download(args[0], 100000) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment