[Python-checkins] cpython: Issue 14989: http.server --cgi option can enable the CGI http server.

senthil.kumaran python-checkins at python.org
Sun Jun 3 10:16:49 CEST 2012


http://hg.python.org/cpython/rev/935a656359ae
changeset: 77320:935a656359ae
parent: 77313:eb1d633fe307
user: Senthil Kumaran <senthil at uthcode.com>
date: Sun Jun 03 16:15:54 2012 +0800
summary:
 Issue 14989: http.server --cgi option can enable the CGI http server.
files:
 Doc/library/http.server.rst | 6 ++++++
 Lib/http/server.py | 22 +++++++++++++++-------
 Misc/NEWS | 3 +++
 3 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -400,3 +400,9 @@
 
 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.
+
+:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing
+the ``--cgi`` option.::
+
+ python -m http.server --cgi 8000
+
diff --git a/Lib/http/server.py b/Lib/http/server.py
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -100,6 +100,8 @@
 import time
 import urllib.parse
 import copy
+import argparse
+
 
 # Default error message template
 DEFAULT_ERROR_MESSAGE = """\
@@ -1173,18 +1175,13 @@
 
 
 def test(HandlerClass = BaseHTTPRequestHandler,
- ServerClass = HTTPServer, protocol="HTTP/1.0"):
+ ServerClass = HTTPServer, protocol="HTTP/1.0", port=8000):
 """Test the HTTP request handler class.
 
 This runs an HTTP server on port 8000 (or the first command line
 argument).
 
 """
-
- if sys.argv[1:]:
- port = int(sys.argv[1])
- else:
- port = 8000
 server_address = ('', port)
 
 HandlerClass.protocol_version = protocol
@@ -1200,4 +1197,15 @@
 sys.exit(0)
 
 if __name__ == '__main__':
- test(HandlerClass=SimpleHTTPRequestHandler)
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--cgi', action='store_true',
+ help='Run as CGI Server')
+ parser.add_argument('port', action='store',
+ default=8000, type=int,
+ nargs='?',
+ help='Specify alternate port [default: 8000]')
+ args = parser.parse_args()
+ if args.cgi:
+ test(HandlerClass=CGIHTTPRequestHandler, port=args.port)
+ else:
+ test(HandlerClass=SimpleHTTPRequestHandler, port=args.port)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Library
 -------
 
+- Issue #14989: Make the CGI enable option to http.server available via command
+ line.
+
 - Issue #14987: Add a missing import statement to inspect.
 
 - Issue #1079: email.header.decode_header now correctly parses all the examples
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /