Skip to content

Commit d36564b

Browse files
committed
Allow pydoc to bind to arbitrary hostnames.
1 parent dadca48 commit d36564b

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Lib/pydoc.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,7 @@ def onerror(modname):
21622162

21632163
# --------------------------------------- enhanced Web browser interface
21642164

2165-
def _start_server(urlhandler, port):
2165+
def _start_server(urlhandler, hostname, port):
21662166
"""Start an HTTP server thread on a specific port.
21672167
21682168
Start an HTML/text server thread, so HTML or text documents can be
@@ -2247,8 +2247,8 @@ def log_message(self, *args):
22472247

22482248
class DocServer(http.server.HTTPServer):
22492249

2250-
def __init__(self, port, callback):
2251-
self.host = 'localhost'
2250+
def __init__(self, host, port, callback):
2251+
self.host = host
22522252
self.address = (self.host, port)
22532253
self.callback = callback
22542254
self.base.__init__(self, self.address, self.handler)
@@ -2268,8 +2268,9 @@ def server_activate(self):
22682268

22692269
class ServerThread(threading.Thread):
22702270

2271-
def __init__(self, urlhandler, port):
2271+
def __init__(self, urlhandler, host, port):
22722272
self.urlhandler = urlhandler
2273+
self.host = host
22732274
self.port = int(port)
22742275
threading.Thread.__init__(self)
22752276
self.serving = False
@@ -2282,7 +2283,7 @@ def run(self):
22822283
DocServer.handler = DocHandler
22832284
DocHandler.MessageClass = email.message.Message
22842285
DocHandler.urlhandler = staticmethod(self.urlhandler)
2285-
docsvr = DocServer(self.port, self.ready)
2286+
docsvr = DocServer(self.host, self.port, self.ready)
22862287
self.docserver = docsvr
22872288
docsvr.serve_until_quit()
22882289
except Exception as e:
@@ -2300,7 +2301,7 @@ def stop(self):
23002301
self.serving = False
23012302
self.url = None
23022303

2303-
thread = ServerThread(urlhandler, port)
2304+
thread = ServerThread(urlhandler, hostname, port)
23042305
thread.start()
23052306
# Wait until thread.serving is True to make sure we are
23062307
# really up before returning.
@@ -2564,14 +2565,14 @@ def get_html_page(url):
25642565
raise TypeError('unknown content type %r for url %s' % (content_type, url))
25652566

25662567

2567-
def browse(port=0, *, open_browser=True):
2568+
def browse(port=0, *, open_browser=True, hostname='localhost'):
25682569
"""Start the enhanced pydoc Web server and open a Web browser.
25692570
25702571
Use port '0' to start the server on an arbitrary port.
25712572
Set open_browser to False to suppress opening a browser.
25722573
"""
25732574
import webbrowser
2574-
serverthread = _start_server(_url_handler, port)
2575+
serverthread = _start_server(_url_handler, hostname, port)
25752576
if serverthread.error:
25762577
print(serverthread.error)
25772578
return
@@ -2618,11 +2619,12 @@ class BadUsage(Exception): pass
26182619
sys.path.insert(0, '.')
26192620

26202621
try:
2621-
opts, args = getopt.getopt(sys.argv[1:], 'bk:p:w')
2622+
opts, args = getopt.getopt(sys.argv[1:], 'bk:h:p:w')
26222623
writing = False
26232624
start_server = False
26242625
open_browser = False
2625-
port = None
2626+
port = 0
2627+
hostname = 'localhost'
26262628
for opt, val in opts:
26272629
if opt == '-b':
26282630
start_server = True
@@ -2635,11 +2637,12 @@ class BadUsage(Exception): pass
26352637
port = val
26362638
if opt == '-w':
26372639
writing = True
2640+
if opt == '-h':
2641+
start_server = True
2642+
hostname = val
26382643

26392644
if start_server:
2640-
if port is None:
2641-
port = 0
2642-
browse(port, open_browser=open_browser)
2645+
browse(port, hostname=hostname, open_browser=open_browser)
26432646
return
26442647

26452648
if not args: raise BadUsage
@@ -2675,6 +2678,9 @@ class BadUsage(Exception): pass
26752678
{cmd} -k <keyword>
26762679
Search for a keyword in the synopsis lines of all available modules.
26772680
2681+
{cmd} -h <hostname>
2682+
Start an HTTP server with the given hostname.(default: localhost)
2683+
26782684
{cmd} -p <port>
26792685
Start an HTTP server on the given port on the local machine. Port
26802686
number 0 can be used to get an arbitrary unused port.

Lib/test/test_pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def my_url_handler(url, content_type):
913913
text = 'the URL sent was: (%s, %s)' % (url, content_type)
914914
return text
915915

916-
serverthread = pydoc._start_server(my_url_handler, port=0)
916+
serverthread = pydoc._start_server(my_url_handler, hostname='localhost', port=0)
917917
self.assertIn('localhost', serverthread.docserver.address)
918918

919919
starttime = time.time()

0 commit comments

Comments
 (0)