通过Find · 2016年5月4日1 看过
之前写过一篇 Python救急HttpServer和Ftpserver ,简单描述了如何开启python内置的httpserver,但是内置的是单线程的,同时只能允许一个人访问。本文则提供了简单的多线程开启httpserver的例程。
import os from threading import Thread import time 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 server.start() time.sleep(2) #Wait to start the server first def test(): url = "http://localhost:" + port_number print(url + " is opened in browser") test()