我有一个 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')
/////////////////////////////////////////////
builtins.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、BReporter、EventHandler
类 NoOpEventHandler(EventHandler):
def on_event(self, event):
self._reporter.on_event(event)
class VerboseNoOpEventHandler(NoOpEventHandler):
FILTER_EVENTS = False
JsonReporter(BReporter) 类:
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()
跑步者 = 跑步([ 'tcp://test123:test123@127.0.0.1:5038', ], 记者, 处理程序 = NoOpEventHandler) runner.run()
答案 0 :(得分:0)
就用这个;您必须设置 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)
打开网络浏览器并复制粘贴:http://127.0.0.1:5000/
答案 1 :(得分:0)
我将您的代码保存在 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)
这意味着代码运行。您可能还有其他问题!
答案 2 :(得分:0)
我认为问题出在 exec(out) 上。实际上,exec是一个程序的动态执行,它可以是一个字符串,也可以是目标代码,它不返回任何东西,它只是执行一个程序并返回None。
如果您在不尝试返回 exec(out) 的情况下运行代码,则不会出错。