Python在线程中启动Simple HttpServer并打开浏览器
示例
如果您的程序在输出网页时很有用。
fromhttp.serverimport HTTPServer, CGIHTTPRequestHandler
import webbrowser
import threading
def start_server(path, port=8000):
'''Start a simple webserver serving path on port'''
os.chdir(path)
httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
httpd.serve_forever()
#在新线程中启动服务器
port = 8000
daemon = threading.Thread(name='daemon_server',
target=start_server,
args=('.', port)
daemon.setDaemon(True) #设置为守护程序,以便一旦主线程死机将被杀死。
daemon.start()
#打开网页浏览器
webbrowser.open('http://localhost:{}'.format(port))
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短