Python Network Programming HTTP Header Information
Python Network Programming: HTTP Headers
Requests and responses between clients and servers involve message headers and messages. The header contains protocol-specific information and appears at the beginning of the original message sent over the TCP connection. The message body is separated from the header by a blank line.
Header Examples
Headers in HTTP responses can be categorized into the following types. Below is a description of the headers and an example.
Cache-Control
The Cache-Control header is generally used to specify directives that all cache systems must obey. Its syntax is as follows.
Cache-Control : cache-request-directive | cache-response-directive
An HTTP client or server can use the Cache-control general-header to specify caching parameters or request certain types of files from the cache. Cache directives are specified as a comma-separated list. For example.
Cache-control: no-cache
Connection
The Connection general-header field allows the sender to specify options required for this particular connection and not to communicate through proxies in other connections. The following is a simple syntax for using the Connection header.
Connection : "Connection"
HTTP/1.1 defines a “Close” connection option for the sender to indicate that the connection will be closed after completing the response. For example.
Connection: close
By default, HTTP 1.1 uses persistent connections, meaning the connection is not automatically closed after a transaction. On the other hand, HTTP 1.0 does not have persistent connections by default. If a 1.0 client wishes to use persistent connections, it can use the keep-alive parameter as shown below.
Connection: keep-alive
Dates
All HTTP date/time stamps must be expressed in Greenwich Mean Time (GMT), without exception. HTTP applications are permitted to use the following three methods for representing date/time stamps.
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
Transfer-Encoding
The Transfer-Encoding general-header field indicates what type of transformation has been applied to the message body in order to securely transmit the information between the sender and receiver. This differs from the content-encoding in that the transfer-encoding is a property of the message, not of the entity-body. The syntax of the Transfer-Encoding header field is as follows.
Transfer-Encoding: chunked
All transfer-encoding values are case-insensitive.
Upgrade
The Upgrade general-header allows a client to specify which additional communication protocols it supports and is used when the server deems it appropriate to switch protocols. For example.
Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
The purpose of the Upgrade header field is to provide a simple mechanism for transitioning from HTTP/1.1 to other incompatible protocols.
Via
The Via general-header MUST be used by gateways and proxies to indicate intermediate protocols and recipients. For example, a request message might be sent from an HTTP/1.0 user agent to an internal proxy codenamed “fred,” which forwards the request using HTTP/1.1 to a public proxy at nowhere.com, which then fulfills the request by forwarding it to the origin server at www.ics.uci.edu. The request received by www.ics.uci.edu would have the following Via header field.
Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
The purpose of the Upgrade header field is to provide a simple mechanism for transitioning from HTTP/1.1 to other incompatible protocols.
Warning
A Warning header is used to carry additional information about the state or transformation of a message that might not be reflected in the message itself. A response may carry more than one Warning header.
Warning: warn-code SP warn-agent SP warn-text SP warn-date
Example
In the following example, we use the urllib2 module to retrieve a response using urlopen. Next, we apply the info() method to retrieve the response header information.
import urllib2
response = urllib2.urlopen('http://www.tutorialspoint.com/python')
html = response.info()
print html
When we run the above program, we get the following output −
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Origin: *
Cache-Control: max-age=2592000
Content-Type: text/html; charset=UTF-8
Date: Mon, 02 Jul 2018 11:06:07 GMT
Expires: Wed, 01 Aug 2018 11:06:07 GMT
Last-Modified: Sun, 01 Jul 2018 21:05:38 GMT
Server: ECS (tir/CDD1)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 22063
Connection: close