MCPcopy Index your code
hub / github.com/python/cpython / _start_server

Function _start_server

Lib/pydoc.py:2285–2435  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2283# --------------------------------------- enhanced web browser interface
2284
2285def _start_server(urlhandler, hostname, port):
2286 """Start an HTTP server thread on a specific port.
2287
2288 Start an HTML/text server thread, so HTML or text documents can be
2289 browsed dynamically and interactively with a web browser. Example use:
2290
2291 >>> import time
2292 >>> import pydoc
2293
2294 Define a URL handler. To determine what the client is asking
2295 for, check the URL and content_type.
2296
2297 Then get or generate some text or HTML code and return it.
2298
2299 >>> def my_url_handler(url, content_type):
2300 ... text = 'the URL sent was: (%s, %s)' % (url, content_type)
2301 ... return text
2302
2303 Start server thread on port 0.
2304 If you use port 0, the server will pick a random port number.
2305 You can then use serverthread.port to get the port number.
2306
2307 >>> port = 0
2308 >>> serverthread = pydoc._start_server(my_url_handler, port)
2309
2310 Check that the server is really started. If it is, open browser
2311 and get first page. Use serverthread.url as the starting page.
2312
2313 >>> if serverthread.serving:
2314 ... import webbrowser
2315
2316 The next two lines are commented out so a browser doesn't open if
2317 doctest is run on this module.
2318
2319 #... webbrowser.open(serverthread.url)
2320 #True
2321
2322 Let the server do its thing. We just need to monitor its status.
2323 Use time.sleep so the loop doesn't hog the CPU.
2324
2325 >>> starttime = time.monotonic()
2326 >>> timeout = 1 #seconds
2327
2328 This is a short timeout for testing purposes.
2329
2330 >>> while serverthread.serving:
2331 ... time.sleep(.01)
2332 ... if serverthread.serving and time.monotonic() - starttime > timeout:
2333 ... serverthread.stop()
2334 ... break
2335
2336 Print any errors that may have occurred.
2337
2338 >>> print(serverthread.error)
2339 None
2340 """
2341 import http.server
2342 import email.message

Callers 1

browseFunction · 0.85

Calls 3

ServerThreadClass · 0.85
startMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…