Python – HTTP Response
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.
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
A server supporting HTTP version 1.1 will return the following version information:
>HTTP-Version = HTTP/1.1
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:
|Code and Description
It means the request was received and the process is continuing.
It means the action was successfully received, understood, and accepted.
It means further action must be taken in order to complete the request.
|4xx: Client Error
It means the request contains incorrect syntax or cannot be fulfilled.
|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