Start the enhanced pydoc web server and open a web browser. Use port '0' to start the server on an arbitrary port. Set open_browser to False to suppress opening a browser.
(port=0, *, open_browser=True, hostname='localhost')
| 2672 | |
| 2673 | |
| 2674 | def browse(port=0, *, open_browser=True, hostname='localhost'): |
| 2675 | """Start the enhanced pydoc web server and open a web browser. |
| 2676 | |
| 2677 | Use port '0' to start the server on an arbitrary port. |
| 2678 | Set open_browser to False to suppress opening a browser. |
| 2679 | """ |
| 2680 | import webbrowser |
| 2681 | serverthread = _start_server(_url_handler, hostname, port) |
| 2682 | if serverthread.error: |
| 2683 | print(serverthread.error) |
| 2684 | return |
| 2685 | if serverthread.serving: |
| 2686 | server_help_msg = 'Server commands: [b]rowser, [q]uit' |
| 2687 | if open_browser: |
| 2688 | webbrowser.open(serverthread.url) |
| 2689 | try: |
| 2690 | print('Server ready at', serverthread.url) |
| 2691 | print(server_help_msg) |
| 2692 | while serverthread.serving: |
| 2693 | cmd = input('server> ') |
| 2694 | cmd = cmd.lower() |
| 2695 | if cmd == 'q': |
| 2696 | break |
| 2697 | elif cmd == 'b': |
| 2698 | webbrowser.open(serverthread.url) |
| 2699 | else: |
| 2700 | print(server_help_msg) |
| 2701 | except (KeyboardInterrupt, EOFError): |
| 2702 | print() |
| 2703 | finally: |
| 2704 | if serverthread.serving: |
| 2705 | serverthread.stop() |
| 2706 | print('Server stopped') |
| 2707 | |
| 2708 | |
| 2709 | # -------------------------------------------------- command-line interface |