Pyjo.Server.CGI - CGI server¶
from Pyjo.Server.CGI import CGI
cgi = CGI()
@cgi.unsubscribe('request').on('request')
def request(cgi, tx):
# Request
method = tx.req.method
path = tx.req.url.path
# Response
tx.res.code = 200
tx.res.headers.content_type = 'text/plain'
tx.res.body = f'{method} request for {path}!'.encode()
# Resume transaction
tx.resume()
cgi.run()
# Or import the module, as in Google Python Style Guide
import Pyjo.Server.CGI
cgi = Pyjo.Server.CGI.new()
@cgi.unsubscribe('request').on('request')
def request(cgi, tx):
# Request
method = tx.req.method
path = tx.req.url.path
# Response
tx.res.code = 200
tx.res.headers.content_type = 'text/plain'
tx.res.body = f'{method} request for {path}!'.encode()
# Resume transaction
tx.resume()
cgi.run()
Pyjo.Server.CGI is a simple and portable implementation of
RFC 3875.
The response can come later, as the event loop of Pyjo.IOLoop runs
while the script waits for it, and the script writes it once the application
resumes the transaction. The body of chunks is written without the chunked
transfer encoding, as a script returns no header that relates to the
connection of the client, which the web server makes, except in non-parsed
header mode, whose response goes to the client as it is. The request body is
read with the length of CONTENT_LENGTH, so a chunked request, which has
none, reaches the application without its body.
CGI servers¶
uWSGI with its plugin
cgi: a chunked request is not answered.http.serverwith--cgi, up to Python 3.14: it sends the output of the script as it is, after a status line200and headers of its own, so the status of the response is lost, and theStatusheader and theDateheader of the script reach the client.
Events¶
Pyjo.Server.CGI inherits all events from Pyjo.Server.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::Server::CGI of Mojolicious 9.49.
Classes¶
- class Pyjo.Server.CGI.Pyjo_Server_CGI(*, nph: bool | None = None, **kwargs: Any)[source]¶
Pyjo.Server.CGIinherits all methods fromPyjo.Serverand implements the following new ones.- nph¶
boolean = cgi.nph cgi.nph = boolean
Activate non-parsed header mode.