26e8fa3f3ff79fe2d257646ed07a72ca908b70a5
David Blume Upgrade to Python 3

David Blume authored 4 years ago

1) #!/usr/bin/env python3
2) # From https://pybay.com/site_media/slides/raymond2017-keynote/process.html
3) import urllib.request
4) import urllib.error
David Blume Compatibility with Python 2.

David Blume authored 8 years ago

5) 
6) sites = [
7) #    'https://www.yahoo.com/',
8) #    'http://www.cnn.com',
9) #    'http://www.python.org',
10) #    'http://www.jython.org',
11)     'http://www.pypy.org',
David Blume Upgrade to Python 3

David Blume authored 4 years ago

12)     'http://www.perl.org',
David Blume Compatibility with Python 2.

David Blume authored 8 years ago

13) #    'http://www.cisco.com',
14) #    'http://www.facebook.com',
15) #    'http://www.twitter.com',
16) #    'http://www.macrumors.com/',
17) #    'http://arstechnica.com/',
18) #    'http://www.reuters.com/',
19) #    'http://abcnews.go.com/',
20) #    'http://www.cnbc.com/',
21) ]
22) 
23) 
David Blume Upgrade to Python 3

David Blume authored 4 years ago

24) def sitesize(url: str):
David Blume Compatibility with Python 2.

David Blume authored 8 years ago

25)     ''' Determine the size of a website '''
David Blume Upgrade to Python 3

David Blume authored 4 years ago

26)     try:
27)         with urllib.request.urlopen(url) as f:
28)             page = f.read()
29)         return url, len(page)
30)     except urllib.error.HTTPError as e:
31)         return url, str(e)