Start http. server with multiple threads in Python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.