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

Skip to content

Commit defe7f4

Browse files
committed
Expose --bind argument for http.server, enable http.server to bind to a user
specified network interface. Patch contributed by Malte Swart. Addresses issue #17764. HG :Enter commit message. Lines beginning with 'HG:' are removed.
1 parent 5642ff9 commit defe7f4

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

Doc/library/http.server.rst

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

369369
python -m http.server 8000
370370

371+
By default, server binds itself to all interfaces. To restrict it to bind to a
372+
particular interface only, ``--bind ADDRESS`` argument can be used. For e.g, to
373+
restrict the server to bind only to localhost. ::
374+
375+
python -m http.server 8000 --bind 127.0.0.1
376+
377+
.. versionadded:: 3.4
378+
``--bind`` argument was introduced.
379+
371380

372381
.. class:: CGIHTTPRequestHandler(request, client_address, server)
373382

Lib/http/server.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,15 +1186,15 @@ def run_cgi(self):
11861186
self.log_message("CGI script exited OK")
11871187

11881188

1189-
def test(HandlerClass = BaseHTTPRequestHandler,
1190-
ServerClass = HTTPServer, protocol="HTTP/1.0", port=8000):
1189+
def test(HandlerClass=BaseHTTPRequestHandler,
1190+
ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):
11911191
"""Test the HTTP request handler class.
11921192
11931193
This runs an HTTP server on port 8000 (or the first command line
11941194
argument).
11951195
11961196
"""
1197-
server_address = ('', port)
1197+
server_address = (bind, port)
11981198

11991199
HandlerClass.protocol_version = protocol
12001200
httpd = ServerClass(server_address, HandlerClass)
@@ -1212,12 +1212,16 @@ def test(HandlerClass = BaseHTTPRequestHandler,
12121212
parser = argparse.ArgumentParser()
12131213
parser.add_argument('--cgi', action='store_true',
12141214
help='Run as CGI Server')
1215+
parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
1216+
help='Specify alternate bind address '
1217+
'[default: all interfaces]')
12151218
parser.add_argument('port', action='store',
12161219
default=8000, type=int,
12171220
nargs='?',
12181221
help='Specify alternate port [default: 8000]')
12191222
args = parser.parse_args()
12201223
if args.cgi:
1221-
test(HandlerClass=CGIHTTPRequestHandler, port=args.port)
1224+
handler_class = CGIHTTPRequestHandler
12221225
else:
1223-
test(HandlerClass=SimpleHTTPRequestHandler, port=args.port)
1226+
handler_class = SimpleHTTPRequestHandler
1227+
test(HandlerClass=handler_class, port=args.port, bind=args.bind)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Core and Builtins
1212
Library
1313
-------
1414

15+
- Issue #17764: Enable http.server to bind to a user specified network
16+
interface. Patch contributed by Malte Swart.
17+
1518
- Issue #18937: Add an assertLogs() context manager to unittest.TestCase
1619
to ensure that a block of code emits a message using the logging module.
1720

0 commit comments

Comments
 (0)