[英]Run Python script from another and pass the variables and output
When looking for a tutorial on passing variables and output between Python scripts I couldn't find any example that would work with the WSGI Server in my example. 在寻找有关在Python脚本之间传递变量和输出的教程时,在我的示例中找不到适用于WSGI Server的任何示例。
I want the output (and variables) returned in HTML instead of seeing it only in the console. 我希望输出(和变量)以HTML返回,而不是仅在控制台中看到。
The best solution for calling python script from another I found is subprocess , but I still can't see the merged output of Script 1 and Script 2 of in my web browser and only in console. 从我发现的另一个脚本中调用python脚本的最佳解决方案是subprocess ,但是我仍然无法在Web浏览器中以及仅在控制台中看到Script 1和Script 2的合并输出。
Script 1: 脚本1:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from cgi import escape
import sys, os
from flup.server.fcgi import WSGIServer
import subprocess
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
yield '<h1>Test - Python pass output and variables</h1>'
yield '<p>Script 1</p>'
yield subprocess.check_output(["python", "script2.py"])
WSGIServer(app).run()
Script 2: 脚本2:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
print "<p>Script 2</p>";
If you want to pass variables between scripts in python, do something like this: 如果要在python中的脚本之间传递变量,请执行以下操作:
Script1.py: Script1.py:
def passVars():
variable = "bla"
return variable
Script2.py: Script2.py:
import Script1 as sc1
var = sc1.passVars()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.