Python中的getheader()函数可以用于获取哪些常见的HTTP头部字段
发布时间:2024-01-11 19:59:05
在Python中,可以使用getheader()函数来获取HTTP响应的头部字段。该函数可以用于获取一些常见的HTTP头部字段,如下所示:
1. 返回服务器类型:"Server"
import http.client
conn = http.client.HTTPSConnection("www.example.com")
conn.request("GET", "/")
response = conn.getresponse()
server_type = response.getheader("Server")
print("Server type:", server_type)
2. 返回内容类型:"Content-Type"
import http.client
conn = http.client.HTTPSConnection("www.example.com")
conn.request("GET", "/")
response = conn.getresponse()
content_type = response.getheader("Content-Type")
print("Content type:", content_type)
3. 返回内容长度:"Content-Length"
import http.client
conn = http.client.HTTPSConnection("www.example.com")
conn.request("GET", "/")
response = conn.getresponse()
content_length = response.getheader("Content-Length")
print("Content length:", content_length)
4. 返回最后修改时间:"Last-Modified"
import http.client
conn = http.client.HTTPSConnection("www.example.com")
conn.request("GET", "/")
response = conn.getresponse()
last_modified = response.getheader("Last-Modified")
print("Last modified:", last_modified)
5. 返回有效期限:"Expires"
import http.client
conn = http.client.HTTPSConnection("www.example.com")
conn.request("GET", "/")
response = conn.getresponse()
expires = response.getheader("Expires")
print("Expires:", expires)
这些是一些常见的HTTP头部字段,可以使用getheader()函数来获取它们。上面的例子展示了如何获取每个字段的值。请注意,这些例子使用了http.client模块,你可能需要根据自己的需求进行修改。
