Clean Code

Python - urllib


urllib and urllib2 are great modules to manipulate http messages. The following example is perfect to make you understand how them. This code is used for an assignment for Coursera: Using Python to Access Web Data. For furhter information, check the Coursera: Using Python to Access Web Data out.

POST request with json body(request/response) and extra headers


import urllib, urllib2
import json

url = ''

# user input and default values for payload
email = raw_input("Enter your email address []:") or ""
password = raw_input("Enter your password [xxxxxxxxx]:") or "xxxxxxxxx"
duration = int(raw_input("Enter duration[30]:") or 30)

import calendar
import datetime
def getFutureUTC(m):
	future = datetime.datetime.utcnow() + datetime.timedelta(minutes = m)
	return calendar.timegm(future.timetuple())
start_time = getFutureUTC(1) # in 1 minute

payload = {"email":email, "password": password, "duration" : duration, "start_time": start_time}

# json payload on request
req = urllib2.Request(url)

# for POST request

# extra headers

response = urllib2.urlopen(req)

print "----------- HEAD --------------"
response_headers = response.headers.headers
for r in response_headers:
	print r.strip()

print "----------- BODY ----------------"
response_data =

js = json.loads(response_data)
print json.dumps(js, indent = 4)
token = js['items'][0]['session_token']
print "session_token", token

Previous : Python - json