杰瑞科技汇

Python httpMessage如何高效处理请求与响应?

Of course! The term "Python HTTPMessage" can refer to a few different things, but most commonly it refers to the http.client.HTTPMessage class from Python's standard library. This class is fundamental for handling HTTP headers.

Python httpMessage如何高效处理请求与响应?-图1
(图片来源网络,侵删)

Let's break it down, starting with the most common meaning and then covering other related concepts.


http.client.HTTPMessage (The Core Class)

This class is part of Python's low-level http.client module, which is used to create HTTP clients (i.e., to make requests to servers). An HTTPMessage object is essentially a container for the headers of an HTTP request or response.

Key Characteristics:

  • It's a dictionary-like object that maps header names (keys) to header values.
  • Header names are case-insensitive, as per the HTTP specification. Content-Type and content-type are treated as the same key.
  • It can handle multiple headers with the same name (like Set-Cookie) by storing them as a list.

How it's Used

You typically don't create an HTTPMessage object yourself. Instead, the http.client library creates it for you when you receive a response from a server.

Python httpMessage如何高效处理请求与响应?-图2
(图片来源网络,侵删)

Example: Making a Request and Inspecting the Headers

Let's make a simple GET request to httpbin.org and see how the HTTPMessage (accessed via response.headers) works.

import http.client
import json
# The target host
host = 'httpbin.org'
path = '/get'
# Create an HTTPS connection (for security)
conn = http.client.HTTPSConnection(host)
try:
    # Send a GET request
    conn.request("GET", path)
    # Get the response from the server
    response = conn.getresponse()
    # --- HERE IS THE HTTPMESSAGE ---
    # response.headers is an instance of http.client.HTTPMessage
    headers = response.headers
    print(f"Status Code: {response.status}")
    print("-" * 30)
    print("Type of response.headers:", type(headers))
    print("-" * 30)
    # You can access headers like a dictionary (case-insensitive)
    print("Content-Type:", headers['Content-Type'])
    print("Server:", headers['Server'])
    print("Date:", headers['Date'])
    # Get all header names
    print("\nAll header names:", headers.keys())
    # Get a specific header, returning a default if not found
    print("X-Amzn-Trace-Id (with default):", headers.get('X-Amzn-Trace-Id', 'Not Found'))
    # You can also iterate over all (name, value) pairs
    print("\n--- All Header Key-Value Pairs ---")
    for key, value in headers.items():
        print(f"{key}: {value}")
    # To get the full response body (as bytes)
    # data = response.read()
    # print(json.loads(data.decode('utf-8')))
finally:
    # Always close the connection
    conn.close()

Running this code will produce output similar to:

Status Code: 200
------------------------------
Type of response.headers: <class 'http.client.HTTPMessage'>
------------------------------
Content-Type: application/json
Server: gunicorn/19.9.0
Date: Wed, 27 Sep 2025 10:30:00 GMT
All header names: ['Content-Type', 'Content-Length', 'Connection', 'Server', 'Date', 'Access-Control-Allow-Origin', 'X-Amzn-Trace-Id']
--- All Header Key-Value Pairs ---
Content-Type: application/json
Content-Length: 329
Connection: close
Server: gunicorn/19.9.0
Date: Wed, 27 Sep 2025 10:30:00 GMT
Access-Control-Allow-Origin: *
X-Amzn-Trace-Id: Root=1-64xxx...

Key Methods of HTTPMessage

  • headers['name'] or headers.get('name', 'default'): Get a header value. Case-insensitive.
  • headers.keys(): Get a list of all header names.
  • headers.items(): Get an iterator of (name, value) pairs.
  • headers.get_all(name): Get a list of all values for a given header name. Useful for headers like Set-Cookie or Cookie that can appear multiple times.
  • headers.add_header(name, value, **params): Add a new header. This is how you can add multiple headers with the same name.

The Modern Alternative: http.HTTPMessage (Python 3.11+)

In Python 3.11, the http package was significantly improved to support HTTP/2 and other modern features. A new, more powerful http.HTTPMessage class was introduced.

Python httpMessage如何高效处理请求与响应?-图3
(图片来源网络,侵删)

This new class is a superset of the old http.client.HTTPMessage and is the future of HTTP header handling in Python. It's designed to be more robust and is used by the new http.HTTPConnection and http.HTTPSConnection.

If you are using Python 3.11 or newer, you will likely encounter this class when working with the updated http library.


http.server.BaseHTTPRequestHandler (Server-Side Context)

When you are writing an HTTP server using Python's http.server module, you also work with HTTP messages, but in a different way.

  • For Requests: The handler object has an attribute self.headers which is an instance of http.client.HTTPMessage. It contains the headers from the client's incoming request.
  • For Responses: You don't typically build a response HTTPMessage object directly. Instead, you use methods like self.send_response(), self.send_header(), and self.end_headers() to construct the response headers line by line.

Example: A Simple Server Reading Request Headers

# Save this as server.py
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # self.headers is an http.client.HTTPMessage object
        # containing the headers from the client's request.
        client_headers = self.headers
        print("--- Received Request Headers ---")
        for name, value in client_headers.items():
            print(f"{name}: {value}")
        print("--------------------------------")
        # Send a 200 OK response
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        # Send a JSON response body
        response_data = {
            "message": "Hello from the server!",
            "your_headers": dict(client_headers)
        }
        self.wfile.write(json.dumps(response_data).encode('utf-8'))
# Run the server
if __name__ == '__main__':
    server_address = ('', 8000)
    httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
    print("Server running on port 8000...")
    httpd.serve_forever()

You can run this server and then make a request to http://localhost:8000 using your browser or a tool like curl. The server's console will print out the headers it received.


High-Level Libraries: You Usually Don't See HTTPMessage

In most real-world applications, you use high-level libraries like requests or httpx. These libraries handle the low-level details for you, including creating and parsing HTTPMessage objects.

The requests library, for example, provides a much more user-friendly requests.structures.CaseInsensitiveDict for its Response.headers attribute, which behaves very similarly to HTTPMessage but is part of the requests ecosystem itself.

Example with requests:

import requests
response = requests.get('https://httpbin.org/get')
# response.headers is a CaseInsensitiveDict, not an http.client.HTTPMessage,
# but it serves the same purpose in a more convenient way.
print(response.headers['Content-Type'])
print(response.headers)

Summary

Context What it is When you'll see it
Client (Low-Level) http.client.HTTPMessage When using http.client to make requests. It's the response.headers object.
Server (Low-Level) http.client.HTTPMessage When using http.server. It's the self.headers attribute in your request handler.
Modern (Python 3.11+) http.HTTPMessage The future, more powerful version, used by the updated http library.
High-Level Libraries (e.g., requests) A dictionary-like object (e.g., requests.structures.CaseInsensitiveDict) that mimics HTTPMessage's behavior. You use it directly as response.headers, but you rarely need to know about the underlying HTTPMessage class.
分享:
扫描分享到社交APP
上一篇
下一篇