Tags

Consume Webservice using python

1. Using requests - Very simple library to consume API

Install requests via pip:

$ pip install virtualenv

Basic Usage

import requests
url = "http://www.example.com?"
data = {'arg1' : value1,'arg2' : value2}
requests.post(url, data=data)

2. Using urllib2 - Very common, standard and inbuilt library to consume API

Basic Usage

import urllib2, urllib
url = "http://www.example.com?"
userAgent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = {'User-Agent': userAgent}
values = {'arg1' : value1,'arg2' : value2}
data = urllib.urlencode(values)
# For GET request
request = urllib2.Request(url+data, None, header)

# For POST request
request = urllib2.Request(url, data, header)

# To call and read response
response = urllib2.urlopen(request)
blog comments powered by Disqus