Start http. server with multiple threads in Python
OS: Windows 8.1 with update Keyword: Python3.4, http. server, Thread example code: import osfrom threading import Threadimport timeimport webbrowser port_number = "8000" def run_on (port): OS. system ("python-m http. server "+ port) if _ name _ =" _ main _ ": server = Thread (target = run_on, args = [port_number]) # run_on (port_number) # Run in main thread # server. daemon = True # Do not make us wait for you to exit ser Ver. start () time. sleep (2) # Wait to start the server first def test (): url = "http: // localhost:" + port_number webbrowser. open (url) print (url + "is opened in browser") test () code explanation: 1. OS. system ("python-m http. server "+ port)," python-m http. server 8000 "is a cmd that can start an http server. 2. server = Thread (target = run_on, args = [port_number]) and create a Thread to start the http server. If http server is started in the main thread, the main thread is blocked and the following code cannot be executed. 3. server. start (), start the thread. 4. time. sleep (2), waiting to start the http server. 5. webbrowser. open (url) to open the url.