What is http response in python?


The http or Hyper Text Transfer Protocol works on client server model. Usually the web browser is the client and the computer hosting the website is the server. Upon receiving a request from client the server generates a response and sends it back to the client in certain format.

After receiving and interpreting a request message, a server responds with an HTTP response message:

  • A Status-line
  • Zero or more header (General|Response|Entity) fields followed by CRLF
  • An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields
  • Optionally a message-body

The following sections explain each of the entities used in an HTTP response message.

Message Status-Line

A Status-Line consists of the protocol version followed by a numeric status code and its associated textual phrase. The elements are separated by space SP characters.

Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

HTTP Version

A server supporting HTTP version 1.1 will return the following version information:

HTTP-Version = HTTP/1.1

Status Code

The Status-Code element is a 3-digit integer where first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit:

S.N.Code and Description
1 1xx: Informational

It means the request was received and the process is continuing.

2 2xx: Success

It means the action was successfully received, understood, and accepted.

3 3xx: Redirection

It means further action must be taken in order to complete the request.

4 4xx: Client Error

It means the request contains incorrect syntax or cannot be fulfilled.

5 5xx: Server Error

It means the server failed to fulfill an apparently valid request.

HTTP status codes are extensible and HTTP applications are not required to understand the meaning of all registered status codes.

Using Python Requests

In the below python program we use the urllib3 module to make a http GET request and receive the response containing the data. It also provides the response code which is also managed by the functions in the module. The PoolManager object handles all of the details of connection pooling and also handles the thread safety.

import urllib3
http = urllib3.PoolManager()

resp = http.request('GET', 'http://tutorialspoint.com/robots.txt')
print resp.data

# get the status of the response
print resp.status

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

User-agent: *
Disallow: /tmp
Disallow: /logs
Disallow: /rate/*
Disallow: /cgi-bin/*
Disallow: /videotutorials/video_course_view.php?*
Disallow: /videotutorials/course_view.php?*
Disallow: /videos/*
Disallow: /*/*_question_bank/*
Disallow: //*/*/*/*/src/*

200

❮ Requests Module


Example

Make a request to a web page, and return the status code:

import requests

x = requests.get('https://w3schools.com')
print(x.status_code)

Run Example »


Definition and Usage

The requests.Response() Object contains the server's response to the HTTP request.


Properties and Methods

Property/MethodDescription
apparent_encoding Try it Returns the apparent encoding
close() Try it Closes the connection to the server
content Try it Returns the content of the response, in bytes
cookies Try it Returns a CookieJar object with the cookies sent back from the server
elapsed Try it Returns a timedelta object with the time elapsed from sending the request to the arrival of the response
encoding Try it Returns the encoding used to decode r.text
headers Try it Returns a dictionary of response headers
history Try it Returns a list of response objects holding the history of request (url)
is_permanent_redirect Try it Returns True if the response is the permanent redirected url, otherwise False
is_redirect Try it Returns True if the response was redirected, otherwise False
iter_content() Try it Iterates over the response
iter_lines() Try it Iterates over the lines of the response
json() Try it Returns a JSON object of the result (if the result was written in JSON format, if not it raises an error)
links Try it Returns the header links
next Try it Returns a PreparedRequest object for the next request in a redirection
ok Try it Returns True if status_code is less than 400, otherwise False
raise_for_status() Try it If an error occur, this method returns a HTTPError object
reason Try it Returns a text corresponding to the status code
request Try it Returns the request object that requested this response
status_code Try it Returns a number that indicates the status (200 is OK, 404 is Not Found)
text Try it Returns the content of the response, in unicode
url Try it Returns the URL of the response

❮ Requests Module


What is HTTP response?

An HTTP response is made by a server to a client. The aim of the response is to provide the client with the resource it requested, or inform the client that the action it requested has been carried out; or else to inform the client that an error occurred in processing its request.

What is HTTP in Python?

http is a package that collects several modules for working with the HyperText Transfer Protocol: http. client is a low-level HTTP protocol client; for high-level URL opening use urllib. request.

What are responses in Python?

When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests. method(), method being – get, post, put, etc.

What is HTTP response in Django?

HttpResponse (source code) provides an inbound HTTP request to a Django web application with a text response. This class is most frequently used as a return object from a Django view.