All Courses

Python - HTTP Client

Jignesh Kumar

a year ago

Http Client | InsideAIML
Table of Contents
  • Introduction
  • Get Initial Response
  • Get Session Object Response
  • Handling Error


          In the HTTP protocol, the request from the client reaches the server and fetches some data and metadata assuming it is a valid request. We can analyze this response from the server using various functions available in the python requests module. Here the below python programs run in the client-side and display the result of the response sent by the server.

Get Initial Response

          In the below program the get method from requests module fetches the data from a server and it is printed in plain text format.

import requests
r = requests.get('')
When we run the above program, we get the following output 
<!DOCTYPE html >
<html lang="en">
  <meta charset="UTF-8">

Get Session Object Response

          The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance. If you’re making several requests to the same host, the underlying TCP connection will be reused.

import requests
s = requests.Session()

r = s.get('')

When we run the above program, we get the following output −


Handling Error

          In case some error is raised because of issue in processing the request by the server, the python program can gracefully handle the exception raised using the timeout parameter as shown below. The program will wait for the defined value of the timeout error and then raise the time out error.

requests.get('', timeout=10.001)
I hope you enjoyed reading this article and finally, you came to know about Python - HTTP Client.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review