20.20. CGIHTTPServer — CGI-capable HTTP request handler - Python 2.7.18 documentation 编辑
Note
The CGIHTTPServer
module has been merged into http.server
in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
The CGIHTTPServer
module defines a request-handler class, interface compatible with BaseHTTPServer.BaseHTTPRequestHandler
and inherits behavior from SimpleHTTPServer.SimpleHTTPRequestHandler
but can also run CGI scripts.
Note
This module can run CGI scripts on Unix and Windows systems.
Note
CGI scripts run by the CGIHTTPRequestHandler
class cannot execute redirects (HTTP code 302), because code 200 (script output follows) is sent prior to execution of the CGI script. This pre-empts the status code.
The CGIHTTPServer
module defines the following class:
- class
CGIHTTPServer.
CGIHTTPRequestHandler
(request, client_address, server) This class is used to serve either files or output of CGI scripts from the current directory and below. Note that mapping HTTP hierarchic structure to local directory structure is exactly as in
SimpleHTTPServer.SimpleHTTPRequestHandler
.The class will however, run the CGI script, instead of serving it as a file, if it guesses it to be a CGI script. Only directory-based CGI are used — the other common server configuration is to treat special extensions as denoting CGI scripts.
The
do_GET()
anddo_HEAD()
functions are modified to run CGI scripts and serve the output, instead of serving files, if the request leads to somewhere below thecgi_directories
path.The
CGIHTTPRequestHandler
defines the following data member:cgi_directories
This defaults to
['/cgi-bin', '/htbin']
and describes directories to treat as containing CGI scripts.
The
CGIHTTPRequestHandler
defines the following methods:do_POST
()This method serves the
'POST'
request type, only allowed for CGI scripts. Error 501, “Can only POST to CGI scripts”, is output when trying to POST to a non-CGI url.
Note that CGI scripts will be run with UID of user nobody, for security reasons. Problems with the CGI script will be translated to error 403.
For example usage, see the implementation of the test()
function.
See also
- Module
BaseHTTPServer
Base class implementation for Web server and request handler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论