Tag Archives: urllib2

Python – urllib2 set proxy

We can set the proxy before making request using urllib2.

proxy = urllib2.ProxyHandler({'http': ''})
opener = urllib2.build_opener(proxy)


Done =)

Reference: StackOverflow – Proxy with urllib2


Python – Read and parse a JSON via URL

Here is an example Python program to read a JSON via URL using urllib2 and simplejson.

import urllib2
import simplejson

response = urllib2.urlopen("")
data = simplejson.load(response)
print data
# => {'content': 'Hello World!', 'success': True}

Continue reading Python – Read and parse a JSON via URL

Python – Open URL with HTTP Authentication using urllib2

Update @ 2015-04-01: Mart suggests using the Requests package. =)

We could add HTTP authentication credential when opening and URL using urllib2.

import urllib2
import base64
import simplejson

username = "<username>"
password = "<password>"
request = urllib2.Request("")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)

data = simplejson.load(response)
print data


Done =)

Reference: StackOverflow – Python urllib2 Basic Auth Problem