{"id":2098,"library":"legacy-cgi","title":"legacy-cgi","description":"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.","status":"maintenance","version":"2.6.4","language":"en","source_language":"en","source_url":"https://github.com/jackrosenthal/legacy-cgi","tags":["cgi","web","legacy","python3.13","compatibility","web development","pep-594"],"install":[{"cmd":"pip install 'legacy-cgi; python_version >= \"3.13\"'","lang":"bash","label":"Conditional install for Python 3.13+"},{"cmd":"pip install legacy-cgi","lang":"bash","label":"Unconditional install"}],"dependencies":[],"imports":[{"symbol":"cgi","correct":"import cgi"},{"symbol":"cgitb","correct":"import cgitb"}],"quickstart":{"code":"# Save this as 'my_cgi_script.py' in your web server's cgi-bin directory\n# and ensure it's executable (e.g., chmod +x my_cgi_script.py)\n\nimport cgi\nimport cgitb\nimport os\n\n# Enable traceback manager for CGI scripts (useful for debugging)\n# In a production environment, consider logging errors instead of displaying them.\ncgitb.enable()\n\nprint(\"Content-Type: text/html\")\nprint() # Empty line signifies end of HTTP headers\n\nprint(\"<!DOCTYPE html>\")\nprint(\"<html>\")\nprint(\"<head><title>Legacy CGI Example</title></head>\")\nprint(\"<body>\")\nprint(\"<h1>Simple CGI Form</h1>\")\n\nform = cgi.FieldStorage()\n\nname = form.getvalue(\"name\", \"Guest\")\nmessage = form.getvalue(\"message\", \"No message provided.\")\n\nif form.getvalue(\"submit\"):\n    print(f\"<p>Hello, <strong>{name}</strong>!</p>\")\n    print(f\"<p>Your message: <em>{message}</em></p>\")\nelse:\n    print(\"<p>Please fill out the form:</p>\")\n\nprint('<form method=\"post\" action=\"my_cgi_script.py\">')\nprint('  <label for=\"name\">Name:</label><br>')\nprint('  <input type=\"text\" id=\"name\" name=\"name\" value=\"\"><br><br>')\nprint('  <label for=\"message\">Message:</label><br>')\nprint('  <textarea id=\"message\" name=\"message\" rows=\"4\" cols=\"50\"></textarea><br><br>')\nprint('  <input type=\"submit\" name=\"submit\" value=\"Submit\">')\nprint('</form>')\n\nprint(\"</body>\")\nprint(\"</html>\")","lang":"python","description":"This example demonstrates a basic CGI script that processes form data using `cgi.FieldStorage`. To run this, save the code as a `.py` file (e.g., `my_cgi_script.py`), make it executable (`chmod +x my_cgi_script.py`), and place it in your web server's configured CGI directory (e.g., `cgi-bin`). The script prints HTTP headers followed by HTML content to the standard output, which the web server then sends to the client."},"warnings":[{"fix":"Install `legacy-cgi` using `pip install legacy-cgi`. For optimal management, use a conditional dependency like `legacy-cgi; python_version >= \"3.13\"` in your `requirements.txt` or `pyproject.toml`.","message":"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.","severity":"breaking","affected_versions":"Python >= 3.13"},{"fix":"The package is primarily for Python 3.13+. If you are on an older Python version and receive deprecation warnings, `legacy-cgi` is not the solution to silence them; you must either upgrade to 3.13+ or explicitly handle warnings.","message":"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.","severity":"gotcha","affected_versions":"Python 3.8 - 3.12"},{"fix":"For new projects, explore modern Python web frameworks like Flask, Django, or FastAPI. This library is intended for maintaining existing CGI-based applications.","message":"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.","severity":"deprecated","affected_versions":"All versions (general architectural advice)"},{"fix":"Do not expect new features or significant architectural improvements within `legacy-cgi`. If advanced web capabilities are needed, migrate to a modern web framework.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}