在网页中显示 python 脚本输出 [server 运行 flask]
display python script output in a web page [server running flask]
我在 Azure 服务器上 运行ning flask 并使用 POST 从表单发送数据作为 python 脚本的参数。
下面是我如何将参数传递给脚本并运行它
os.system("python3 script.py " + postArgument)
输出在日志中正常显示,就像在终端上一样。
如何将输出返回到新网页?
你可以使用 pipe ,这里是如何完成的
os.popen("python3 script.py " + postArgument).read()
从安全角度来看,我建议您在使用
之前对 postArguements
进行一些健全性检查
编辑:回答询问为什么进行完整性检查的评论
代码易受命令注入
Command injection is an attack in which the goal is execution of
arbitrary commands on the host operating system via a vulnerable
application. Command injection attacks are possible when an
application passes unsafe user supplied data (forms, cookies, HTTP
headers etc.) to a system shell. In this attack, the attacker-supplied
operating system commands are usually executed with the privileges of
the vulnerable application. Command injection attacks are possible
largely due to insufficient input validation.
让我尝试在您的案例中演示可能的攻击
如果
postArgument = "blah ; rm -rf /"
然后
os.popen("python3 script.py " + postArgument).read()
将等于
os.popen("python3 script.py blah ; rm -rf /").read()
这将尝试删除系统中的所有文件。
如何避免这种情况
要么使用 pipes.Quote
import pipes
p = os.popen("python3 script.py " + pipes.quote(postArgument)).read()
或使用subprocess
,推荐使用os.popen
,因为
import subprocess
p = subprocess.Popen(["python3", "script.py", postArguemnt])
阅读here关于命令注入的内容
我在 Azure 服务器上 运行ning flask 并使用 POST 从表单发送数据作为 python 脚本的参数。
下面是我如何将参数传递给脚本并运行它
os.system("python3 script.py " + postArgument)
输出在日志中正常显示,就像在终端上一样。
如何将输出返回到新网页?
你可以使用 pipe ,这里是如何完成的
os.popen("python3 script.py " + postArgument).read()
从安全角度来看,我建议您在使用
之前对postArguements
进行一些健全性检查
编辑:回答询问为什么进行完整性检查的评论
代码易受命令注入
Command injection is an attack in which the goal is execution of arbitrary commands on the host operating system via a vulnerable application. Command injection attacks are possible when an application passes unsafe user supplied data (forms, cookies, HTTP headers etc.) to a system shell. In this attack, the attacker-supplied operating system commands are usually executed with the privileges of the vulnerable application. Command injection attacks are possible largely due to insufficient input validation.
让我尝试在您的案例中演示可能的攻击 如果
postArgument = "blah ; rm -rf /"
然后
os.popen("python3 script.py " + postArgument).read()
将等于
os.popen("python3 script.py blah ; rm -rf /").read()
这将尝试删除系统中的所有文件。
如何避免这种情况
要么使用 pipes.Quote
import pipes
p = os.popen("python3 script.py " + pipes.quote(postArgument)).read()
或使用subprocess
,推荐使用os.popen
,因为
import subprocess
p = subprocess.Popen(["python3", "script.py", postArguemnt])
阅读here关于命令注入的内容