我正在尝试创建一个程序,该程序将使用Bottle模块启动服务器,然后使用webbrowser模块打开服务器URL。启动瓶子服务器后打开Web浏览器时出现问题。如果我在启动瓶子服务器之前打开Web浏览器,则Web浏览器成功打开。但是,如果我在打开Web浏览器之前启动了瓶子服务器,则Web浏览器将永远不会打开。
import webbrowser
from bottle import route, run, request
def step1():
url = 'http://localhost:8080'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s --incognito'
webbrowser.get(chrome_path).open_new(url)
run(host='', port=8080)
step1()
我希望服务器启动,然后打开Web浏览器。但是,Web浏览器只是无法打开,并且不会引发任何错误。
在此示例中,在运行服务器之前打开Web浏览器即可,并且连接成功。但是,如果我想做一个更复杂的Web浏览器功能,需要服务器的反馈,它将无法正常工作。
启动瓶服务器后是否可以打开Web浏览器?
谢谢!
在控制台中使用此脚本,打开您的Chrome并将其指向http://localhost:8181/hello/Russell
import webbrowser
from bottle import route, run, template
@route('/hello/<name>')
def index(name):
url = 'http://localhost:8181/hello/Russell'
chrome_path = '"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" %s --incognito'
webbrowser.get(chrome_path).open_new(url)
return template('<b>Hello, <font color=red size=+2>{{name}}</font></b>!', name=name)
run(host='localhost', port=8181)