Using Requests in Python

Hi guys, hope you all are having wonderful time here. Lets explore into some more advanced side of Python. In this part of our tutorial we will send HTTP/1.1 requests, this can be accomplished importing request module in python.

Using Requests in Python

As i mentioned in the intro section that using requests module in python we can send HTTP/1.1 request. Its easy to install this module by using either pip or easy_install. So whenever we try to get a webpage using requests.get() method we get a Response object using which we can get all the informations that we need.

Let learn basics of this with the help of examples :

Now i will make request for a webpage so here i will be using our site http://www.pythonlovers.net/ and get the server Response.

Making a request and getting the response code

>>> import requests
>>> req = requests.get("http://www.pythonlovers.net/")
>>> print req.status_code
200
>>> req.status_code == requests.codes.ok
True

In the above example, we have made a request to a website and got the Response object “req”.

Then to verify if the request is successful we check the response status code, if the code is 200 it means that the “Request is Fulfilled”.

Requests also comes with a built-in status code lookup object for easy reference like we have used “requests.codes.ok”.

To get the Server’s content

>>> print req.text
<!DOCTYPE html>
<html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns#">
<head itemscope itemtype="http://schema.org/WebSite">
 <meta charset="UTF-8">
 <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame -->
 <!--[if IE ]>
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <![endif]-->
 <link rel="profile" href="http://gmpg.org/xfn/11" />
      
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="apple-mobile-web-app-capable" content="yes">
            <meta name="apple-mobile-web-app-status-bar-style" content="black">
        
        
        <meta itemprop="name" content="PythonLovers" /> . . . . . .

here we get the entire source code of the website when we use print req.text

Headers

We can view the server’s response header using a Python dictionary, it is also possible to access the headers using any capitalisation.

If buy chance header does not exist in the Server’s Response, then it’s default value is none.

Example :

>>> req.headers
{'wp-super-cache': 'Served supercache file from PHP', 'content-length': '10897', 'content-encoding': 'gzip', 'x-served-from-cache': 'Yes', 'accept-ranges': 'bytes', 'vary': 'Accept-Encoding,Cookie', 'x-cacheable': 'YES', 'server': 'nginx/1.8.1', 'connection': 'keep-alive', 'cache-control': 'max-age=3, must-revalidate', 'date': 'Sun, 27 Mar 2016 08:08:58 GMT', 'content-type': 'text/html; charset=UTF-8'}
>>> req.headers['connection']
'keep-alive'
>>> req.headers.get('connection')
‘keep-alive'

Use other HTTP requests

So in previous examples and explanations we have used the HTTP Get method, if you remember there are other HTTP requests types like POST, PUT, DELETE, HEAD and OPTIONS.

In python such requests using this module can be used in the following way as shown in the example.

>>> req = requests.put("http://httpbin.org/put")
>>> req = requests.delete("http://httpbin.org/delete")
>>> req = requests.head("http://httpbin.org/get")
>>> req = requests.options(“http://httpbin.org/get”)

Errors and Exceptions

ConnectionError, HTTPError, Timeout, TooManyRedirects — These are some of the Exceptions that you might encounter when you work on this module and all exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.

ConnectionError – In the event of a network problem (e.g. DNS failure, refused connection, etc).

HTTPError – In the rare event of an invalid HTTP response.

Timeout – If a request times out.

TooManyRedirects – If a request exceeds the configured number of maximum redirections.

We gave you the Overview of requests module that will help you to start your Exploration on this, but you can refer to the very well documentation of Requests, for which you have to refer this link

http://docs.python-requests.org/en/master/user/quickstart/

So in case of any queries or question do reach us, We will help you in best possible way to keep the things going in you favor, Thank you.

One Response

  1. isc April 11, 2016