Skip to content

Instantly share code, notes, and snippets.

@edsion1107
Created May 22, 2019 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edsion1107/93c82e2a38f306aa7e7d9a12c56764e4 to your computer and use it in GitHub Desktop.
Save edsion1107/93c82e2a38f306aa7e7d9a12c56764e4 to your computer and use it in GitHub Desktop.
通过python调用浏览器,打开一个本地html
#!/usr/bin/env python
# encoding: utf-8
"""
@author: edsion
@file: demo.py
@time: 2019-05-22 14:50
当前工程目录结构:
.
├── allure-report_1108126771
│   ├── app.js
│   ├── data
│   ├── export
│   ├── favicon.ico
│   ├── history
│   ├── index.html
│   ├── plugins
│   ├── styles.css
│   └── widgets
├── demo.py
"""
import os
import webbrowser
from threading import Thread
from pathlib import Path
from http.server import test, SimpleHTTPRequestHandler
os.chdir(Path('allure-report_1108126771'))
Thread(target=test, kwargs={'HandlerClass': SimpleHTTPRequestHandler}).start()
# time.sleep(3) # http server启动需要一点时间
webbrowser.get('chrome').open('http://localhost:8000/index.html')
@edsion1107
Copy link
Author

如果是python3.7,可以指定--directory参数,从而省略os.chdir()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment