我有一个python脚本(script.py(,当我在控制台中运行它时,它一直以JSON格式接收数据,现在我想在网页上可视化控制台的输出,或者能够执行script.py并捕获输出以在浏览器中查看,从我所读到的内容来看,我理解使用FLASK我可以做到这一点,这是实现它的一个示例或指南。
from flask import Flask
app = Flask(__name__)
@app.route("/")
def code():
out = open(r'output.py', 'r').read()
return exec(out)
if __name__ == '__main__':
app.run(host='0.0.0.0')
///////////////////////////////////////////////
内置。运行时错误
RuntimeError:线程"thread-9"中没有当前事件循环。追踪(最近一次通话(
File "..//python3.6/site-packages/flask/app.py", line 2301, in __call__
return self.wsgi_app(environ, start_response)
File "..//python3.6/site-packages/flask/app.py", line 2287, in wsgi_app
response = self.handle_exception(e)
File "..//python3.6/site-packages/flask/app.py", line 1733, in handle_exception
reraise(exc_type, exc_value, tb)
File "..//python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "..//python3.6/site-packages/flask/app.py", line 2284, in wsgi_app
response = self.full_dispatch_request()
File "..//python3.6/site-packages/flask/app.py", line 1807, in full_dispatch_request
rv = self.handle_user_exception(e)
File "..//python3.6/site-packages/flask/app.py", line 1710, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "..//python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "..//python3.6/site-packages/flask/app.py", line 1805, in full_dispatch_request
rv = self.dispatch_request()
File "..//python3.6/site-packages/flask/app.py", line 1791, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/share/server/www/test/index1.py", line 9, in dynamic_page
return exec(file)
File "<string>", line 60, in <module>
///////////////////////////////////////////////////////////////////////////输出.py
#/usr/bin/env python3
导入json导入系统
从手机导入Runn、BReport、EventHandler
类NoOpEventHandler(EventHandler(:
def on_event(self, event):
self._reporter.on_event(event)
类VerboseNoOpEventHandler(NoOpEventHandler(:
FILTER_EVENTS = False
class JsonReporter(B报表(:
def __init__(self):
self._has_first_line = False
def on_event(self, event):
json_data = json.dumps(dict(event), sort_keys=True)
if self._has_first_line:
sys.stdout.write(',')
else:
sys.stdout.write('[')
self._has_first_line = True
sys.stdout.write('n {}'.format(json_data))
def close(self):
sys.stdout.write('n]')
reporter=JsonReporter((
runner=跑步(['tcp://test123:test123@127.0.0.1:5038’,],reporter,handler=NoOpEventHandler(runner.run((
只需使用此;必须设置debug = True
。您不必为测试提供主机。
if __name__ == "__main__":
app.run(debug=True)
您应该在终端上看到类似的输出;
* Serving Flask app "my_flask_app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: xxx-xxx-xxx
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
打开web浏览器并复制粘贴:http://127.0.0.1:5000/
我将您的代码保存在tmp.py
中,并将其作为python tmp.py
运行。
from flask import Flask
app = Flask(__name__)
@app.route("/")
def code():
out = open(r'output.py', 'r').read()
return exec(out)
if __name__ == '__main__':
app.run(debug = True)
按如下方式运行:
(flaskvenv) C:...>python tmp.py
这是输出:
* Serving Flask app "tmp" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: XXX-XXX-XXX
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
这意味着代码会运行。你可能还有其他问题!
我认为问题出在exec(out(上。实际上,exec是一个程序的动态执行,它可以是字符串,也可以是对象代码,它不返回任何东西,它只是执行一个程序并返回None。
如果您运行代码而不尝试返回exec(out(,则不会出现错误。