Skip to main content
 首页 » 编程设计

python之在 CherryPy 中启动浏览器

2024年08月12日32artech

我有一个使用...显示的 html 页面

cherrypy.quickstart(ShowHTML(htmlfile), config=configfile) 

页面加载后(例如通过命令“python mypage.py”启动),我想自动启动浏览器以显示页面(例如通过http://localhost/8000)。有什么方法可以实现这一点(例如,通过 CherryPy 中的一个钩子(Hook)),还是我必须手动调用浏览器(例如,通过双击图标)?

TIA 艾伦

请您参考如下方法:

您可以将您的网络浏览器挂接到引擎启动/停止生命周期:

def browse(): 
    webbrowser.open("http://127.0.0.1:8080") 
cherrypy.engine.subscribe('start', browse, priority=90) 

或者,解压快速入门:

from cherrypy import config, engine, tree 
 
config.update(configfile) 
tree.mount(ShowHTML(htmlfile), '/', configfile) 
if hasattr(engine, "signal_handler"): 
    engine.signal_handler.subscribe() 
if hasattr(engine, "console_control_handler"): 
    engine.console_control_handler.subscribe() 
 
engine.start() 
webbrowser.open("http://127.0.0.1:8080")     
engine.block()