Start an HTTP server thread on a specific port. Start an HTML/text server thread, so HTML or text documents can be browsed dynamically and interactively with a web browser. Example use: >>> import time >>> import pydoc Define a URL handler. To determine what the
(urlhandler, hostname, port)
| 2331 | # --------------------------------------- enhanced web browser interface |
| 2332 | |
| 2333 | def _start_server(urlhandler, hostname, port): |
| 2334 | """Start an HTTP server thread on a specific port. |
| 2335 | |
| 2336 | Start an HTML/text server thread, so HTML or text documents can be |
| 2337 | browsed dynamically and interactively with a web browser. Example use: |
| 2338 | |
| 2339 | >>> import time |
| 2340 | >>> import pydoc |
| 2341 | |
| 2342 | Define a URL handler. To determine what the client is asking |
| 2343 | for, check the URL and content_type. |
| 2344 | |
| 2345 | Then get or generate some text or HTML code and return it. |
| 2346 | |
| 2347 | >>> def my_url_handler(url, content_type): |
| 2348 | ... text = 'the URL sent was: (%s, %s)' % (url, content_type) |
| 2349 | ... return text |
| 2350 | |
| 2351 | Start server thread on port 0. |
| 2352 | If you use port 0, the server will pick a random port number. |
| 2353 | You can then use serverthread.port to get the port number. |
| 2354 | |
| 2355 | >>> port = 0 |
| 2356 | >>> serverthread = pydoc._start_server(my_url_handler, port) |
| 2357 | |
| 2358 | Check that the server is really started. If it is, open browser |
| 2359 | and get first page. Use serverthread.url as the starting page. |
| 2360 | |
| 2361 | >>> if serverthread.serving: |
| 2362 | ... import webbrowser |
| 2363 | |
| 2364 | The next two lines are commented out so a browser doesn't open if |
| 2365 | doctest is run on this module. |
| 2366 | |
| 2367 | #... webbrowser.open(serverthread.url) |
| 2368 | #True |
| 2369 | |
| 2370 | Let the server do its thing. We just need to monitor its status. |
| 2371 | Use time.sleep so the loop doesn't hog the CPU. |
| 2372 | |
| 2373 | >>> starttime = time.monotonic() |
| 2374 | >>> timeout = 1 #seconds |
| 2375 | |
| 2376 | This is a short timeout for testing purposes. |
| 2377 | |
| 2378 | >>> while serverthread.serving: |
| 2379 | ... time.sleep(.01) |
| 2380 | ... if serverthread.serving and time.monotonic() - starttime > timeout: |
| 2381 | ... serverthread.stop() |
| 2382 | ... break |
| 2383 | |
| 2384 | Print any errors that may have occurred. |
| 2385 | |
| 2386 | >>> print(serverthread.error) |
| 2387 | None |
| 2388 | """ |
| 2389 | import http.server |
| 2390 | import email.message |
no test coverage detected