Open In App
Related Articles

Network Programming Python – HTTP Clients

Improve Article
Save Article
Like Article

The request from the client in HTTP protocol reaches the server and fetches some data assuming it to be a valid request. This response from the server can be analyzed by using various methods provided by the requests module. Some of the ways below provide information about the response sent from the server to the Python program running on the client-side :

Getting initial Response

The get() method is used to get the basic information of the resources used at the server-side. This function fetches the data from the server and returns as a response object which can be printed in a simple text format.


# Import libraries
import requests
# Sending Request
req = requests.get('')
# Show results


Getting session-info

The session() method returns the session object which provides certain parameters to manipulate the requests. It can also manipulate cookies for all the requests initiated from the session object. If a large amount of requests are made to a single host then, the associated TCP connection is recalled.


# Import Libraries
import requests
# Creating Session
s = requests.Session()
# Getting Response
# Show Response


  "cookies": {
    "sessioncookie": "419735271"

Error Handling

If some error is raised in processing the request to the server, the exception is raised by the program which can be handled using the timeout attribute, which defines the time value until the program waits and after that, it raises the timeout error.


# Import Libraries
import requests
# Error Handling
    # Creating Request
    req = requests.get('', timeout=0.000001)
except requests.exceptions.RequestException as e:
    # Raising Error
    raise SystemExit(e)


HTTPSConnectionPool(host=’’, port=443): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x0000018CFDAD83C8>, ‘Connection to timed out. (connect timeout=1e-06)’))

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 Oct, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials