pycgi is a lightweight web server solution that allows you to embed Python code directly into HTML files, similar to how PHP works. By using <?py ?> tags, you can dynamically generate web pages with the power of Python.
- Embed Python in HTML: Use
<?py ?>tags to embed Python code inside your HTML files. - Dynamic Content Generation: Generate dynamic web pages by mixing static HTML and Python logic.
- Simple and Intuitive: Just like PHP, no need for complex configurations. Write HTML and Python together in one file.
- Request Handling: Access request headers, query parameters, and POST data easily.
- Response Manipulation: Set response headers and body content directly from your Python code.
- Flexible Output: Use the
echofunction to render content directly to the output stream at any point in your HTML. - Execution Path Access: Use
_EXECUTE_PATHto display the execution path of the running pycgi instance. - Document Root Access: Use
_DOCSROOTto get the document root directory where your.pycgifiles are located. - Storage API: Easily store and retrieve key-value pairs using the
Storageobject.
Here’s an example of how you can use pycgi to handle request data, set response headers, and use the echo function, including displaying the execution path and document root:
<!DOCTYPE html>
<html>
<head>
<title>My pycgi Page</title>
</head>
<body>
<h1>Welcome to pycgi!</h1>
<!-- Static HTML content -->
<p>This is a static message.</p>
<!-- Embedded Python code -->
<?py
# Set a response header
_RSP_HEADERS["Content-Type"] = "text/html; charset=utf-8"
_RSP_HEADERS["Custom-Header"] = "MyHeaderValue"
# Display the execution path
echo(f"<p>Execution Path: {_EXECUTE_PATH}</p>")
# Display the document root
echo(f"<p>Document Root: {_DOCSROOT}</p>")
# Use echo to render content
echo("<p>This is rendered using echo.</p>")
# Print request headers
for header, value in _REQ_HEADERS.items():
echo(f"<p>{header}: {value}</p>")
# Print GET query parameter
if "name" in _GET:
echo(f"<p>Hello, {_GET['name']}!</p>")
# Print POST data
if _POST_DATA:
echo("<p>POST Data:</p><pre>")
echo(_POST_DATA)
echo("</pre>")
# Print JSON POST data
if _POST_JSON:
echo("<p>JSON POST Data:</p><pre>")
echo(_POST_JSON)
echo("</pre>")
# Storage API Example
# Set a value in the storage
Storage.setValue("nickname", "pycgi")
# Retrieve the value from the storage with a default value
nickname = Storage.getValue("nickname", "none")
echo(f"<p>Stored Nickname: {nickname}</p>")
?>
</body>
</html>In the above example:
- Storage API: Demonstrates how to store and retrieve values using the
Storageobject withStorage.setValue(key, value)andStorage.getValue(key, default)methods. - Execution Path: Display the current execution path of the pycgi instance using
_EXECUTE_PATH. - Document Root: Display the document root directory using
_DOCSROOT. - Response Headers: Set response headers using the
_RSP_HEADERSdictionary. - Dynamic Output: Use the
echofunction to render HTML content directly to the output stream, allowing you to inject content at any point in your HTML.
-
Download the latest version of pycgi from the Release page.
-
Once downloaded, you can run the server by simply double-clicking on
pycgi../pycgi
When you run this command, a
docsfolder will automatically be created in the same directory. You can place your.pycgifiles inside this folder.
-
Inside the
docsfolder, create a new file namedindex.pycgiwith the following content:<!DOCTYPE html> <html> <head> <title>My First pycgi Page</title> </head> <body> <h1>Hello pycgi!</h1> <?py echo("<p>This is Python inside HTML!</p>") ?> </body> </html>
-
Open your web browser and visit
http://localhost:8000/index.pycgito see your newly created page.
pycgi now includes a simple Storage API that allows you to store and retrieve key-value pairs during the execution of your web application.
-
Store a Value: You can store a value in the storage by calling
Storage.setValue(key, value).Storage.setValue("nickname", "pycgi")
-
Retrieve a Value: To retrieve a value from the storage, use
Storage.getValue(key, default). If the key is not found, it will return the default value you provide.nickname = Storage.getValue("nickname", "none") echo(f"Stored Nickname: {nickname}")
This feature is useful for temporarily storing data during the execution of a request, such as user session information or other dynamic content.
pycgi allows you to handle various types of requests seamlessly:
-
Request Headers: Access headers via the
_REQ_HEADERSdictionary.echo(_REQ_HEADERS["Header-Name"])
-
GET Parameters: Access GET parameters using the
_GETdictionary.echo(_GET["parameter_name"])
-
POST Data: Access raw POST data through the
_POST_DATAvariable.echo(_POST_DATA)
-
JSON POST Data: If you send JSON data in the POST request, access it via the
_POST_JSONvariable.echo(_POST_JSON["key_name"])
-
echo(_EXECUTE_PATH)
echo(_PATH)
echo(_DOCS_ROOT)
echo(_LISTEN_PORT)
You can modify the HTTP response before it is sent back to the client:
-
Set Response Headers: Add or modify response headers using the
_RSP_HEADERSdictionary._RSP_HEADERS["Header-Name"] = "Header Value"
-
Set Response Body: You can define the response body using the
_RSP_BODYvariable. However, usingechoallows you to output content directly within your HTML structure._RSP_BODY = "<h2>This is the response body.</h2>"
-h, --help show this help message and exit
--port PORT Port to run the server onpycgi processes files with a .pycgi extension, scanning for any Python code blocks between <?py ?> tags. It executes the Python code and replaces the tags with the corresponding output in the HTML, rendering the final result to the user’s browser.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or feedback, feel free to open an issue or contact me at nnnnnnn0090@gmail.com.