Connecting Python to the Web at UT Southwestern

While working on a little Python script to do  some datamining for lab, I stumbled upon a serious problem:

When connecting to the Internet via the school’s network, whether hard-wired, wifi, or through VPN, I could not get Python to fetch webpages using urllib.request. Here’s the quick solution to anyone else with that problem (for the general solution, just change the proxy address and port to your own).

import urllib.request as web
proxy_url, proxy_port = 'http://proxy.swmed.edu', '3128'
proxy_handler = urllib.request.ProxyHandler({'http':proxy_url+':'+proxy_port})
new_opener = web.build_opener(proxy_handler)
page = new_opener.open( url )

I actually just reassigned urllib.request.urlopen = opener.open so that the syntax would stay the same. I got the info from the Python3.1 documentation page for urllib.request, using minor changes to one of their examples.

Leave a Comment

Filed under computers/software, HowTo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s