Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1251faf

Browse files
committed
Issue 14989: http.server --cgi option can enable the CGI http server.
1 parent c68e136 commit 1251faf

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

Doc/library/http.server.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,9 @@ the previous example, this serves files relative to the current directory. ::
400400

401401
Note that CGI scripts will be run with UID of user nobody, for security
402402
reasons. Problems with the CGI script will be translated to error 403.
403+
404+
:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing
405+
the ``--cgi`` option.::
406+
407+
python -m http.server --cgi 8000
408+

Lib/http/server.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
import time
101101
import urllib.parse
102102
import copy
103+
import argparse
104+
103105

104106
# Default error message template
105107
DEFAULT_ERROR_MESSAGE = """\
@@ -1173,18 +1175,13 @@ def run_cgi(self):
11731175

11741176

11751177
def test(HandlerClass = BaseHTTPRequestHandler,
1176-
ServerClass = HTTPServer, protocol="HTTP/1.0"):
1178+
ServerClass = HTTPServer, protocol="HTTP/1.0", port=8000):
11771179
"""Test the HTTP request handler class.
11781180
11791181
This runs an HTTP server on port 8000 (or the first command line
11801182
argument).
11811183
11821184
"""
1183-
1184-
if sys.argv[1:]:
1185-
port = int(sys.argv[1])
1186-
else:
1187-
port = 8000
11881185
server_address = ('', port)
11891186

11901187
HandlerClass.protocol_version = protocol
@@ -1200,4 +1197,15 @@ def test(HandlerClass = BaseHTTPRequestHandler,
12001197
sys.exit(0)
12011198

12021199
if __name__ == '__main__':
1203-
test(HandlerClass=SimpleHTTPRequestHandler)
1200+
parser = argparse.ArgumentParser()
1201+
parser.add_argument('--cgi', action='store_true',
1202+
help='Run as CGI Server')
1203+
parser.add_argument('port', action='store',
1204+
default=8000, type=int,
1205+
nargs='?',
1206+
help='Specify alternate port [default: 8000]')
1207+
args = parser.parse_args()
1208+
if args.cgi:
1209+
test(HandlerClass=CGIHTTPRequestHandler, port=args.port)
1210+
else:
1211+
test(HandlerClass=SimpleHTTPRequestHandler, port=args.port)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Beta 1?
1010
Library
1111
-------
1212

13+
- Issue #14989: Make the CGI enable option to http.server available via command
14+
line.
15+
1316
- Issue #14987: Add a missing import statement to inspect.
1417

1518
- Issue #1079: email.header.decode_header now correctly parses all the examples

0 commit comments

Comments
 (0)