@@ -16,12 +16,15 @@ class or function within a module or module in a package. If the
16
16
Run "pydoc -k <keyword>" to search for a keyword in the synopsis lines
17
17
of all available modules.
18
18
19
+ Run "pydoc -n <hostname>" to start an HTTP server with the given
20
+ hostname (default: localhost) on the local machine.
21
+
19
22
Run "pydoc -p <port>" to start an HTTP server on the given port on the
20
23
local machine. Port number 0 can be used to get an arbitrary unused port.
21
24
22
25
Run "pydoc -b" to start an HTTP server on an arbitrary unused port and
23
- open a Web browser to interactively browse documentation. The -p option
24
- can be used with the -b option to explicitly specify the server port.
26
+ open a Web browser to interactively browse documentation. Combine with
27
+ the -n and -p options to control the hostname and port used .
25
28
26
29
Run "pydoc -w <name>" to write out the HTML documentation for a module
27
30
to a file named "<name>.html".
@@ -2162,7 +2165,7 @@ def onerror(modname):
2162
2165
2163
2166
# --------------------------------------- enhanced Web browser interface
2164
2167
2165
- def _start_server (urlhandler , port ):
2168
+ def _start_server (urlhandler , hostname , port ):
2166
2169
"""Start an HTTP server thread on a specific port.
2167
2170
2168
2171
Start an HTML/text server thread, so HTML or text documents can be
@@ -2247,8 +2250,8 @@ def log_message(self, *args):
2247
2250
2248
2251
class DocServer (http .server .HTTPServer ):
2249
2252
2250
- def __init__ (self , port , callback ):
2251
- self .host = 'localhost'
2253
+ def __init__ (self , host , port , callback ):
2254
+ self .host = host
2252
2255
self .address = (self .host , port )
2253
2256
self .callback = callback
2254
2257
self .base .__init__ (self , self .address , self .handler )
@@ -2268,8 +2271,9 @@ def server_activate(self):
2268
2271
2269
2272
class ServerThread (threading .Thread ):
2270
2273
2271
- def __init__ (self , urlhandler , port ):
2274
+ def __init__ (self , urlhandler , host , port ):
2272
2275
self .urlhandler = urlhandler
2276
+ self .host = host
2273
2277
self .port = int (port )
2274
2278
threading .Thread .__init__ (self )
2275
2279
self .serving = False
@@ -2282,7 +2286,7 @@ def run(self):
2282
2286
DocServer .handler = DocHandler
2283
2287
DocHandler .MessageClass = email .message .Message
2284
2288
DocHandler .urlhandler = staticmethod (self .urlhandler )
2285
- docsvr = DocServer (self .port , self .ready )
2289
+ docsvr = DocServer (self .host , self . port , self .ready )
2286
2290
self .docserver = docsvr
2287
2291
docsvr .serve_until_quit ()
2288
2292
except Exception as e :
@@ -2304,7 +2308,7 @@ def stop(self):
2304
2308
self .serving = False
2305
2309
self .url = None
2306
2310
2307
- thread = ServerThread (urlhandler , port )
2311
+ thread = ServerThread (urlhandler , hostname , port )
2308
2312
thread .start ()
2309
2313
# Wait until thread.serving is True to make sure we are
2310
2314
# really up before returning.
@@ -2568,14 +2572,14 @@ def get_html_page(url):
2568
2572
raise TypeError ('unknown content type %r for url %s' % (content_type , url ))
2569
2573
2570
2574
2571
- def browse (port = 0 , * , open_browser = True ):
2575
+ def browse (port = 0 , * , open_browser = True , hostname = 'localhost' ):
2572
2576
"""Start the enhanced pydoc Web server and open a Web browser.
2573
2577
2574
2578
Use port '0' to start the server on an arbitrary port.
2575
2579
Set open_browser to False to suppress opening a browser.
2576
2580
"""
2577
2581
import webbrowser
2578
- serverthread = _start_server (_url_handler , port )
2582
+ serverthread = _start_server (_url_handler , hostname , port )
2579
2583
if serverthread .error :
2580
2584
print (serverthread .error )
2581
2585
return
@@ -2622,11 +2626,12 @@ class BadUsage(Exception): pass
2622
2626
sys .path .insert (0 , '.' )
2623
2627
2624
2628
try :
2625
- opts , args = getopt .getopt (sys .argv [1 :], 'bk:p:w' )
2629
+ opts , args = getopt .getopt (sys .argv [1 :], 'bk:n: p:w' )
2626
2630
writing = False
2627
2631
start_server = False
2628
2632
open_browser = False
2629
- port = None
2633
+ port = 0
2634
+ hostname = 'localhost'
2630
2635
for opt , val in opts :
2631
2636
if opt == '-b' :
2632
2637
start_server = True
@@ -2639,11 +2644,12 @@ class BadUsage(Exception): pass
2639
2644
port = val
2640
2645
if opt == '-w' :
2641
2646
writing = True
2647
+ if opt == '-n' :
2648
+ start_server = True
2649
+ hostname = val
2642
2650
2643
2651
if start_server :
2644
- if port is None :
2645
- port = 0
2646
- browse (port , open_browser = open_browser )
2652
+ browse (port , hostname = hostname , open_browser = open_browser )
2647
2653
return
2648
2654
2649
2655
if not args : raise BadUsage
@@ -2679,14 +2685,17 @@ class BadUsage(Exception): pass
2679
2685
{cmd} -k <keyword>
2680
2686
Search for a keyword in the synopsis lines of all available modules.
2681
2687
2688
+ {cmd} -n <hostname>
2689
+ Start an HTTP server with the given hostname (default: localhost).
2690
+
2682
2691
{cmd} -p <port>
2683
2692
Start an HTTP server on the given port on the local machine. Port
2684
2693
number 0 can be used to get an arbitrary unused port.
2685
2694
2686
2695
{cmd} -b
2687
2696
Start an HTTP server on an arbitrary unused port and open a Web browser
2688
- to interactively browse documentation. The -p option can be used with
2689
- the -b option to explicitly specify the server port .
2697
+ to interactively browse documentation. This option can be used in
2698
+ combination with -n and/or -p .
2690
2699
2691
2700
{cmd} -w <name> ...
2692
2701
Write out the HTML documentation for a module to a file in the current
0 commit comments