杰瑞科技汇

Python startresponse如何正确使用?

Of course! Let's break down startresponse in Python, specifically in the context of web servers.

Python startresponse如何正确使用?-图1
(图片来源网络,侵删)

What is startresponse?

startresponse is a function that your application (a WSGI application) must call as part of handling an incoming web request. It's a core concept in the WSGI (Web Server Gateway Interface) standard, which is the conventional way for Python web applications to communicate with web servers.

Think of it as your application's way of talking back to the web server to say, "Okay, I've processed the request. Here's the HTTP status I want to send back, and here are the headers."

The Role of startresponse in the WSGI Flow

To understand startresponse, you need to see it in the context of the WSGI interaction:

  1. Client Request: A web browser (or any client) sends an HTTP request to a web server (like Gunicorn, uWSGI, or the built-in wsgiref server).
  2. Server to App: The web server receives the request and invokes your Python application (a callable, usually a function or an object's __call__ method). It passes two arguments:
    • environ: A dictionary containing all the information about the request (e.g., PATH_INFO, REQUEST_METHOD, HTTP_USER_AGENT).
    • startresponse: The function you need to call to begin the HTTP response.
  3. App Prepares Response: Your application does its work (e.g., queries a database, renders a template).
  4. App Calls startresponse: Your application calls the startresponse function to set the status code and headers.
  5. App Returns Body: Your application then returns an iterable (like a list of strings or a generator) that yields the body of the HTTP response.
  6. Server to Client: The web server takes the status, headers, and body returned by your app and constructs the final HTTP response, which it sends back to the client.

The Signature of startresponse

The startresponse function has this signature:

Python startresponse如何正确使用?-图2
(图片来源网络,侵删)
startresponse(status, response_headers, exc_info=None)

Let's break down each parameter:

status (Required)

This is a string that represents the HTTP status code and reason phrase. It must be in the format "CODE PHRASE".

  • Common Examples:
    • "200 OK": Success.
    • "404 Not Found": Resource not found.
    • "500 Internal Server Error": A server-side error occurred.
    • "301 Moved Permanently": For redirects.

response_headers (Required)

This is a list of (key, value) tuples that make up the HTTP headers. These headers provide metadata about the response.

  • Common Examples:
    • [('Content-Type', 'text/html; charset=utf-8')]: Tells the browser to expect HTML content.
    • [('Content-Type', 'application/json')]: Tells the browser to expect JSON.
    • [('Content-Length', '123')]: The size of the response body in bytes.
    • [('Location', 'https://example.com/new-page')]: Used for redirects (often with a 301 or 302 status).

exc_info (Optional)

This is used for error handling. If your application encounters an error after it has already called startresponse, you can't just raise the exception. Instead, you can call startresponse again, this time passing the exception information as the third argument.

Python startresponse如何正确使用?-图3
(图片来源网络,侵删)
  • Example: exc_info=sys.exc_info()
  • This allows the server to send a proper error page (e.g., a 500 error) instead of crashing the connection. For most simple applications, you won't need to use this.

A Complete, Simple Example

Here is a minimal, runnable WSGI application that demonstrates how to use startresponse.

# A simple WSGI application
def simple_app(environ, start_response):
    """
    This is a very basic WSGI application.
    """
    # 1. Prepare the status and headers
    status = '200 OK'
    response_headers = [
        ('Content-Type', 'text/plain; charset=utf-8'),
        ('X-Powered-By', 'WSGI Demo')
    ]
    # 2. Call startresponse
    # This tells the server we are starting the response.
    start_response(status, response_headers)
    # 3. Return an iterable (the body of the response)
    # The body must be an iterable of bytes.
    response_body = b"Hello, WSGI World!\n"
    return [response_body]
# --- How to run this application ---
# Use the built-in WSGI server from the standard library
from wsgiref.simple_server import make_server
# Create a server on port 8000, running our simple_app
httpd = make_server('', 8000, simple_app)
print("Serving on port 8000...")
# Go into a loop to handle requests
httpd.serve_forever()

How to Run and Test It:

  1. Save the code above as my_app.py.
  2. Run it from your terminal: python my_app.py
  3. Open your web browser and go to http://localhost:8000.
  4. You will see the text: "Hello, WSGI World!"
  5. You can also inspect the headers using command-line tools like curl:
    $ curl -I http://localhost:8000
    HTTP/1.0 200 OK
    Date: ...
    Server: WSGIRef/0.2 Python/3.10.x
    Content-Type: text/plain; charset=utf-8
    X-Powered-By: WSGI Demo
    Content-Length: 19

Key Points to Remember

  1. Mandatory Call: You must call startresponse exactly once before you start returning the response body. Calling it more than once (without exc_info) is an error.
  2. Order Matters: You call startresponse before you return the body iterable. This allows the server to start sending headers to the client before it receives the potentially large response body.
  3. It's a Function, Not a Variable: Even though it's passed as an argument, startresponse is a function provided by the server. You don't define it; you just call it.
  4. Not Used in Modern Frameworks: If you use a modern web framework like Flask, Django, or FastAPI, you will never see startresponse in your application code. These frameworks handle the WSGI boilerplate for you. You just return data from your view functions, and the framework takes care of calling startresponse and formatting the final response. Understanding it is crucial for understanding how these frameworks work under the hood.
分享:
扫描分享到社交APP
上一篇
下一篇