Python Network Programming 简明教程
Python - HTTP Headers
客户端和服务器之间的请求和响应在消息中包含头和主体。头包含特定于协议的信息,显示在通过 TCP 连接发送的原始消息的开头。消息的正文使用空白行从头中分离出来。
The request and response between client and server involves header and body in the message. Headers contain protocol specific information that appear at the beginning of the raw message that is sent over TCP connection. The body of the message is separated from headers using a blank line.
Example of Headers
http 响应中的头可以分为以下类型。以下是对头和示例的描述。
The headers in the http response can be categorized into following types. Below is a description of the header and an example.
Cache-Control
Cache-Control通用头字段用于指定所有缓存系统都必须遵守的指令。以下是语法:
The Cache-Control general-header field is used to specify directives that MUST be obeyed by all the caching system. The syntax is as follows:
Cache-Control : cache-request-directive|cache-response-directive
HTTP客户端或服务器可以使用`@ {s0}`通用标头为缓存指定参数,或从缓存中请求某些类型的文档。缓存指令已逗号分隔列表中指定。例如:
An HTTP client or server can use the Cache-control general header to specify parameters for the cache or to request certain kinds of documents from the cache. The caching directives are specified in a comma-separated list. For example:
Cache-control: no-cache
Connection
Connection通用头字段允许发送方指定该特定连接所需选项,并且不能通过代理在进一步连接中传达这些选项。以下是使用连接标头的简单语法:
The Connection general-header field allows the sender to specify options that are desired for that particular connection and must not be communicated by proxies over further connections. Following is the simple syntax for using connection header:
Connection : "Connection"
HTTP/1.1为发送方定义了“close”连接选项,以表示在完成响应后将关闭该连接。例如:
HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example:
Connection: close
默认情况下,HTTP 1.1使用持久连接,其中连接不会在事务后自动关闭。另一方面,HTTP 1.0默认情况下没有持久连接。如果1.0客户端希望使用持久连接,它将使用`@ {s1}`参数,如下所示:
By default, HTTP 1.1 uses persistent connections, where the connection does not automatically close after a transaction. HTTP 1.0, on the other hand, does not have persistent connections by default. If a 1.0 client wishes to use persistent connections, it uses the keep-alive parameter as follows:
Connection: keep-alive
Date
所有HTTP日期/时间戳都必须以格林尼治标准时间(GMT)表示,无一例外。HTTP应用程序可以使用以下三种日期/时间戳表示形式中的任何一种:
All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. HTTP applications are allowed to use any of the following three representations of 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
Transfer-Encoding 通用头字段指示为了在发送方和接收方之间安全地传输消息正文而对消息正文应用了哪种类型的转换。这与 content-encoding 不同,因为传输编码是消息的属性,而不是实体正文的属性。Transfer-Encoding 头字段的语法如下:
The Transfer-Encoding general-header field indicates what type of transformation has been applied to the message body in order to safely transfer it between the sender and the recipient. This is not the same as content-encoding because transfer-encodings are a property of the message, not of the entity-body. The syntax of Transfer-Encoding header field is as follows:
Transfer-Encoding: chunked
所有传输编码值不区分大小写。
All transfer-coding values are case-insensitive.
Upgrade
Upgrade 通用标题允许客户端指定其支持的其他通信协议,如果服务器发现切换协议合适,则客户端希望使用这些协议。例如:
The Upgrade general-header allows the client to specify what additional communication protocols it supports and would like to use if the server finds it appropriate to switch protocols. For example:
Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
Upgrade 头字段旨在为从 HTTP/1.1 过渡到其他不兼容协议提供一种简单的机制。
The Upgrade header field is intended to provide a simple mechanism for transition from HTTP/1.1 to some other, incompatible protocol.
Via
网关和代理必须使用 Via 通用头字段来指示中间协议和接收者。例如,请求消息可以从 HTTP/1.0 用户代理发送到内部代理(代号为“fred”),该代理使用 HTTP/1.1 将请求转发到 nowhere.com 上的公共代理,该代理通过将请求转发到 www.ics.uci.edu 上的原始服务器来完成请求。然后,www.ics.uci.edu 收到的请求将具有以下 Via 头字段:
The Via general-header must be used by gateways and proxies to indicate the intermediate protocols and recipients. For example, a request message could be sent from an HTTP/1.0 user agent to an internal proxy code-named "fred", which uses HTTP/1.1 to forward the request to a public proxy at nowhere.com, which completes the request by forwarding it to the origin server at www.ics.uci.edu. The request received by www.ics.uci.edu would then have the following Via header field:
Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Upgrade 头字段旨在为从 HTTP/1.1 过渡到其他不兼容协议提供一种简单的机制。
The Upgrade header field is intended to provide a simple mechanism for transition from HTTP/1.1 to some other, incompatible protocol.
Warning
Warning 通用标题用于携带有关消息状态或转换的其他信息,这些信息可能不会反映在消息中。响应可能携带多个 Warning 标题。
The Warning general-header is used to carry additional information about the status or transformation of a message which might not be reflected in the message. A response may carry more than one Warning header.
Warning : warn-code SP warn-agent SP warn-text SP warn-date
Example
在以下示例中,我们使用 urllib2 模块来 get 一个使用 urlopen 的响应。接下来,我们应用 info() 方法来获得该响应的头信息。
In the below example we use the urllib2 module to get a response using urlopen. Next we apply the info() method to get the header information for that response.
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