杰瑞科技汇

Python getheaders如何获取请求头信息?

Of course! In Python, "getheaders" can refer to a few different things depending on the context. I'll cover the most common ones, starting with the classic Python 2 method and moving to the modern, recommended approaches in Python 3.

Python getheaders如何获取请求头信息?-图1
(图片来源网络,侵删)

The Classic: urllib.request.getheaders() (Python 2 & 3)

This is the method most people think of when they hear "getheaders". It's part of the older urllib library.

Important Note: This method was deprecated in Python 3.9 and removed in Python 3.12. If you are using a modern version of Python (3.10+), this code will not work.

What it did:

It returned the headers of an HTTP response as a list of (name, value) tuples.

Example (Python 2 / Python 3 < 3.9):

import urllib.request
# The URL to fetch
url = 'http://example.com'
try:
    # Open the URL and get a response object
    response = urllib.request.urlopen(url)
    # Get the headers as a list of tuples
    headers = response.getheaders()
    print("Headers (list of tuples):")
    print(headers)
    print("-" * 20)
    # You can iterate through them
    print("Iterating through headers:")
    for header in headers:
        print(header)
except Exception as e:
    print(f"An error occurred: {e}")

Output:

Headers (list of tuples):
[('Date', 'Wed, 26 Oct 2025 10:00:00 GMT'), ('Server', 'Apache'), ('Content-Length', '1270'), ('Content-Type', 'text/html; charset=UTF-8')]
--------------------
Iterating through headers:
('Date', 'Wed, 26 Oct 2025 10:00:00 GMT')
('Server', 'Apache')
('Content-Length', '1270')
('Content-Type', 'text/html; charset=UTF-8')

The Modern Way: requests Library (Recommended)

The requests library is the de facto standard for making HTTP requests in Python. It's much more user-friendly and powerful than the built-in urllib.

Python getheaders如何获取请求头信息?-图2
(图片来源网络,侵删)

To use it, you first need to install it:

pip install requests

In requests, headers are accessed as a dictionary-like object on the response object. This is much more convenient than a list of tuples.

How to Get Headers with requests

The response object's headers attribute is a special CaseInsensitiveDict, which means you can access headers using any case (e.g., response.headers['Content-Type'] or response.headers['content-type']).

Example:

import requests
url = 'http://example.com'
try:
    # Make a GET request
    response = requests.get(url)
    # The response object has a 'headers' attribute
    headers = response.headers
    print("Headers (CaseInsensitiveDict):")
    # Printing the dict directly is clean
    print(headers)
    print("-" * 20)
    # Access a specific header by key
    content_type = headers['Content-Type']
    print(f"Content-Type: {content_type}")
    # Access another header
    server = headers.get('Server') # Use .get() for safety
    print(f"Server: {server}")
except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

Output:

Headers (CaseInsensitiveDict):
{'Content-Type': 'text/html; charset=UTF-8', 'Content-Length': '1270', 'Server': 'Apache', 'Date': 'Wed, 26 Oct 2025 10:00:00 GMT', 'Connection': 'keep-alive', 'ETag': '"62-5d1a5d8a4f6c4"', 'Last-Modified': 'Wed, 14 Aug 2025 15:44:10 GMT', 'Accept-Ranges': 'bytes', 'Cache-Control': 'max-age=604800'}
--------------------
Content-Type: text/html; charset=UTF-8
Server: Apache

The Built-in Python 3 Way: http.client

If you don't want to use the requests library and are on a modern Python version (3.9+), you can use the lower-level http.client module. This is what urllib uses under the hood.

Python getheaders如何获取请求头信息?-图3
(图片来源网络,侵删)

This approach is more verbose but gives you fine-grained control.

Example:

import http.client
url = 'example.com'
# Note: http.client requires the host and port separately
# For standard HTTP, port is 80. For HTTPS, it's 443 and you'd use http.client.HTTPSConnection
try:
    # Create a connection object
    conn = http.client.HTTPConnection(url)
    # Send a GET request
    conn.request("GET", "/")
    # Get the response object
    response = conn.getresponse()
    # In http.client, headers are accessed via the getheaders() method
    # This is the direct successor to the old urllib method
    headers = response.getheaders()
    print(f"Status: {response.status} {response.reason}")
    print("-" * 20)
    print("Headers (list of tuples):")
    print(headers)
    # Don't forget to close the connection
    conn.close()
except Exception as e:
    print(f"An error occurred: {e}")

Output:

Status: 200 OK
--------------------
Headers (list of tuples):
[('Date', 'Wed, 26 Oct 2025 10:00:00 GMT'), ('Server', 'Apache'), ('Content-Length', '1270'), ('Content-Type', 'text/html; charset=UTF-8'), ('Connection', 'close')]

Summary: Which one should you use?

Method Library Python Version How to Access Headers Recommendation
urllib.request.getheaders() urllib Python 2, Python 3 < 3.9 response.getheaders() Avoid. It's deprecated and removed. Use only for legacy code.
requests requests (external) All modern Python versions response.headers (a dict-like object) Highly Recommended. The standard, easiest, and most powerful way.
http.client.getheaders() http.client All modern Python versions response.getheaders() (a list of tuples) Use if you can't install external libraries and need low-level control.

For any new project, use the requests library. It simplifies everything and is the industry standard.

分享:
扫描分享到社交APP
上一篇
下一篇