CGI in Python

The CGI standard lets you use any language to code CGI scripts. Python is a very high-level, high-productivity language, and thus quite suitable for CGI coding. The Python standard library supplies modules to handle typical CGI-related tasks.

Form Submission Methods

CGI scripts often handle submitted HTML forms. In this case, the action attribute of the form tag specifies the URL for a CGI script to handle the form, and the method attribute is GET or POST, indicating how the form data is sent to the script. According to the CGI standard, the GET method should be used only for forms without side effects, such as asking the server to query a database and display results, while the POST method is meant for forms with side effects, such as asking the server to update a database. In practice, however, GET is also often used to create side effects. The distinction between GET and POST in practical use is that GET encodes the form’s contents as a query string joined to the action URL to form a longer URL, while POST transmits the form’s contents as an encoded stream of data, which a CGI script sees as standard input.

GET is slightly faster. You can use a fixed GET-form URL wherever you can use a hyperlink. However, GET cannot send large amounts of data to the server, since many clients and servers limit URL lengths (you’re safe up to about 200 bytes). The POST method has no size limits. You must use POST when the form contains input tags with type=file—the form tag must then ...

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.