我正在尝试启动一个简单的 HTTP 服务器,然后在默认浏览器中打开它。我不知道我做错了什么,它要么根本不启动服务器,要么一旦到达脚本末尾就停止(它不是应该永远运行吗?)。
import BaseHTTPServer, SimpleHTTPServer, webbrowser, thread
def start_server():
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 3600), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.serve_forever()
thread.start_new_thread(start_server,())
url = 'http://127.0.0.1:3600'
webbrowser.open_new(url)
只要应用程序继续运行,线程就会继续存在,在这种情况下webbrowser.open_new()
不是阻塞的,所以浏览器很难完成应用程序的运行,你应该做的是创建一个阻止程序来阻止应用程序完成执行:
import sys
import thread
import webbrowser
import time
import BaseHTTPServer, SimpleHTTPServer
def start_server():
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 3600), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.serve_forever()
thread.start_new_thread(start_server,())
url = 'http://127.0.0.1:3600'
webbrowser.open_new(url)
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
sys.exit(0)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)