English | Site Directory

Requests and CGI

When App Engine receives a web request for your application, it calls the handler script that corresponds to the URL, as described in the application's app.yaml configuration file. App Engine uses the CGI standard to communicate the request data to the handler, and receive the response.

URL Mapping and Handler Scripts

When App Engine receives a web request for your application, it routes the request to a web server. The web server checks the application's app.yaml file for a URL mapping that matches the URL of the request to determine which Python handler script will handle the request.

App Engine uses multiple web servers to run your application, and automatically adjusts the number of servers it is using to handle requests reliably. A given request may be routed to any server, and it may not be the same server that handled a previous request from the same user.

CGI

Once the request has been routed to a server and the appropriate handler script, App Engine calls the script. As described in the CGI standard, the server puts the request data in environment variables and the standard input stream. The script performs actions appropriate to the request, then prepares a response and puts it on the standard output stream.

You can refer to the CGI documentation for details about the environment variables and the format of the input stream data. Most applications use a library to parse CGI requests and return CGI responses, such as the cgi module from the Python standard library, or a web framework that knows the CGI protocol (such as webapp).

The following example handler script displays a message on the user's browser. It prints an HTTP header that identifies the type of the message and the content of message to the standard output stream.

print "Content-Type: text/plain"
print ""
print "Hello, world!"

Streaming Is Not Supported

App Engine collects all of the data the handler script writes to the standard output stream, then waits for the script to exit. When the script exits, all of the output data is sent to the user. App Engine does not support sending data to the user's browser before exiting the handler.

Responses are Compressed Automatically

If the client sends HTTP headers with the request indicating that the client can accept compressed (gzip'd) content, App Engine compresses the response data automatically and attaches the appropriate response headers. Both the Accept-Encoding and User-Agent request headers are used to determine if the client can reliably receive compressed responses. Custom clients can force content to be compressed by specifying both Accept-Encoding and User-Agent headers with a value of "gzip".