Python HTTP module – All you need to know!

Python HTTP Module

hello, readers! In this article, we will be focusing on the Python HTTP module, in detail. So, let us begin!! 🙂

Recommended read: ReLU function in Python


Crisp Overview – Python HTTP module

Python is a multi-purpose programming language that helps us perform various operations at different levels of hierarchy at ease. With python module comes in a huge variety of modules and built-in functions to perform classic and customized/user-defined operations.

When it comes to data scraping, or fetching the information through APIs or JSON data paths, we need functions that enable us to open a connection to the web URL and then perform response operations on the same.

Python offers us with HTTP module. With the HTTP module, we can easily handle the web URL connections and perform various operations such as:

  1. GET request
  2. POST request
  3. PUT request
  4. Fetching headers from the response headers, etc

We will be having a look at each of the above-mentioned functions with the HTTP module. The HTTP module is often clubbed with the urllib module to deal with HTTP requests at the latest times.

Let us begin!!


1. Establish an HTTP connection

Before performing any request operation with a web URL, it is very important to establish a connection with the URL. In the HTTP module, the HTTPConnection() function enables us to open a connection to the URL on a specific port (preferably 80) with a time-out period.

Syntax:

http.client.HTTPConnection('URL', port, timeout=)
  • URL: The web URL with which the connection is to be established.
  • port: The port number on which the connection needs to be established.
  • timeout: The grace period within which the connection would be aborted.

Example:

import http.client
request = http.client.HTTPConnection('www.google.com', 80, timeout=10)
print(request)

Output:

<http.client.HTTPConnection object at 0x00000223BAD2DDD8>

2. Python HTTP GET request

With the HTTP module, we can perform GET requests to the web URL which we can use to get the response from the web URL. Using GET response, we establish a give-away connection with the web URL and get the response data served by the URL, and assigns an object to represent it.

Further, the response data can be also validated using the reason and status attributes of the request() function.

Syntax:

request("GET")

Example:

import http.client

data = http.client.HTTPSConnection("www.askpython.com")
data.request("GET", "/")
response = data.getresponse()
print(response.reason)
print(response.status)
data.close()

Output:

OK
200

3. Python HTTP Post & Put request

Apart from HTTP GET request, we can also use POST request that enables us to inject data, i.e. post data to the URL and then get the response from the URL using a GET request.

Further, if we wish to modify and add certain data to the JSON data of the URL/API, we can do so using a PUT request. With a PUT request, we can add data to the existing JSON of the URL and check against its connection using a GET request.

SyntaxPOST request:

request('POST', '/post', json_data, headers)

Syntax – PUT request:

request("PUT", "/put", json_data)

4. Retrieving header list from response

Once you establish a connection with a web URL and request a GET response, we can now extract and retrieve the header data from the response available using the getheaders() function. The getheaders() function represents the list of header data from the GET response.

Syntax:

request.getheaders()

Example:

import http.client

data = http.client.HTTPSConnection("www.askpython.com")
data.request("GET", "/")
response = data.getresponse()
header = response.getheaders()

print(header)
print(response.reason)
print(response.status)
data.close()

Output–

[('Connection', 'Keep-Alive'), ('Content-Type', 'text/html; charset=UTF-8'), ('Link', '<https://www.askpython.com/wp-json/>; rel="https://api.w.org/"'), ('Link', '</wp-content/themes/astra/assets/css/minified/style.min.css>; rel=preload; as=style,</wp-content/themes/astra/assets/css/minified/menu-animation.min.css>; rel=preload; as=style,</wp-includes/css/dist/block-library/style.min.css>; rel=preload; as=style,</wp-content/plugins/wp-to-twitter/css/twitter-feed.css>; rel=preload; as=style,</wp-content/plugins/easy-table-of-contents/vendor/icomoon/style.min.css>; rel=preload; as=style,</wp-content/plugins/easy-table-of-contents/assets/css/screen.min.css>; rel=preload; as=style,</wp-content/themes/obsidian/style.css>; rel=preload; as=style'), ('Etag', '"294191-1623490484;;;"'), ('X-LiteSpeed-Cache', 'hit'), ('Transfer-Encoding', 'chunked'), ('Date', 'Sun, 13 Jun 2021 07:30:37 GMT'), ('Server', 'LiteSpeed')]
OK 
200


Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 🙂