Recently, I’ve been trying to wrap my head around using Python tools behind a proxy. I found that the documentation specific to Mac OS X was sparse so I decided to post my findings.

First, if you’ve configured proxy settings in System Preferences you’ll find that urllib2 retrieves your settings using the ‘OS X System Configuration Framework’:
>>> import urllib
>>> urllib.getproxies()

{'http': 'http://proxy.institution.edu:8080', 'https': 'http://proxy.institution.edu:8080'}

You can use an opener with urllib2 to handle your own proxy settings:
>>> import urllib2
>>> request = urllib2.Request('http://institution.edu')

>>> proxy_handler = urllib2.ProxyHandler({'http': 'http://proxy.institution.edu:8080', 'https': 'http://proxy.institution.edu:8080'})

>>> opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1), proxy_handler)
>>> opener.open(request).read()

Lastly, Python also honors your bash proxy exports, including ‘no_proxy’ settings:
export {http,https,ftp}_proxy='http://proxy.institution.edu:8080'
export no_proxy='institution.edu'

>>> import urllib
>>> urllib.getproxies()
{'ftp': 'http://proxy.institution.edu:8080', 'http': 'http://proxy.institution.edu:8080', 'https': 'http://proxy.institution.edu:8080', 'no': 'institution.edu'}

And of course, read the docs to learn more: http://docs.python.org/2/library/urllib2.html