]> granicus.if.org Git - python/commitdiff
Issue 14989: http.server --cgi option can enable the CGI http server.
authorSenthil Kumaran <senthil@uthcode.com>
Sun, 3 Jun 2012 08:15:54 +0000 (16:15 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Sun, 3 Jun 2012 08:15:54 +0000 (16:15 +0800)
Doc/library/http.server.rst
Lib/http/server.py
Misc/NEWS

index d947ed2a5bef86ac26142ded958f9382e58b5f2d..04ec746e2da1232eea225c23fc320160997c1be1 100644 (file)
@@ -400,3 +400,9 @@ the previous example, this serves files relative to the current directory. ::
 
    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
+
index cb66f2b63cb8cc954a816aa22fe7f82f0d8705dd..c4ac703d2daf0f6cc4ef1b945219ed7c614492d6 100644 (file)
@@ -100,6 +100,8 @@ import sys
 import time
 import urllib.parse
 import copy
+import argparse
+
 
 # Default error message template
 DEFAULT_ERROR_MESSAGE = """\
@@ -1173,18 +1175,13 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
 
 
 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 @@ def test(HandlerClass = BaseHTTPRequestHandler,
         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)
index b74696981a1736c32b80e7d2a048dc4f70019401..e732604ae8f0f13c787eff32620539f4da5e0204 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 1?
 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