legacy-cgi
legacy-cgi is a fork of the standard library `cgi` and `cgitb` modules, which were removed from Python's standard library in version 3.13 as per PEP 594. Its primary purpose is to provide a drop-in replacement to support existing CGI scripts and applications that rely on these modules, ensuring compatibility for Python 3.13+. The current version is 2.6.4, with development focused on maintaining compatibility and applying bug fixes rather than introducing new features.
Warnings
- breaking The `cgi` and `cgitb` modules were entirely removed from the Python standard library in Python 3.13, following deprecation in Python 3.11 (PEP 594). Code relying on these modules will break without `legacy-cgi` installed on Python 3.13+ environments.
- gotcha On Python versions prior to 3.13 (e.g., 3.11, 3.12), the standard library's `cgi` module still exists and takes precedence. Installing `legacy-cgi` on these versions will not automatically replace the standard library module unless the import path is explicitly manipulated, and it will not silence `DeprecationWarning`s from the standard library module.
- deprecated CGI (Common Gateway Interface) is considered a legacy technology for web development. Modern web applications should generally use contemporary WSGI (e.g., Flask, Django) or ASGI (e.g., FastAPI) frameworks, which offer significantly better performance, security, and development features. CGI typically launches a new Python process for each request, leading to high overhead.
- gotcha The `legacy-cgi` project aims for compatibility and bug fixes, not feature enhancements or major refactoring. Its API will closely mirror the final version of the `cgi` and `cgitb` modules found in Python 3.12.
Install
-
pip install 'legacy-cgi; python_version >= "3.13"' -
pip install legacy-cgi
Imports
- cgi
import cgi
- cgitb
import cgitb
Quickstart
# Save this as 'my_cgi_script.py' in your web server's cgi-bin directory
# and ensure it's executable (e.g., chmod +x my_cgi_script.py)
import cgi
import cgitb
import os
# Enable traceback manager for CGI scripts (useful for debugging)
# In a production environment, consider logging errors instead of displaying them.
cgitb.enable()
print("Content-Type: text/html")
print() # Empty line signifies end of HTTP headers
print("<!DOCTYPE html>")
print("<html>")
print("<head><title>Legacy CGI Example</title></head>")
print("<body>")
print("<h1>Simple CGI Form</h1>")
form = cgi.FieldStorage()
name = form.getvalue("name", "Guest")
message = form.getvalue("message", "No message provided.")
if form.getvalue("submit"):
print(f"<p>Hello, <strong>{name}</strong>!</p>")
print(f"<p>Your message: <em>{message}</em></p>")
else:
print("<p>Please fill out the form:</p>")
print('<form method="post" action="my_cgi_script.py">')
print(' <label for="name">Name:</label><br>')
print(' <input type="text" id="name" name="name" value=""><br><br>')
print(' <label for="message">Message:</label><br>')
print(' <textarea id="message" name="message" rows="4" cols="50"></textarea><br><br>')
print(' <input type="submit" name="submit" value="Submit">')
print('</form>')
print("</body>")
print("</html>")