Tag Archives: urllib2

Python – urllib2 set proxy

We can set the proxy before making request using urllib2.

proxy = urllib2.ProxyHandler({'http': '127.0.0.1'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com')

&nvbsp;

Done =)

Reference: StackOverflow – Proxy with urllib2

Advertisements

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("http://172.0.0.1:8000/custom/get/")
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("http://127.0.0.1:8000/custom/get/")
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