在Python CGI脚本中使用OS.System()
我正在我的Web主机上使用Python构建一个CGI脚本,并使用WGET对其进行测试。 Shebang #!/HOME/ME/ME/virtualenv/public_html/Chartex/3.6/bin/python3.6
将其指向正确的Python版本sys.executable)在CGI中正确返回:
3.6.15 (default, Jan 14 2022, 12:16:54)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] /home/me/virtualenv/public_html/chartex/3.6/bin/python3.6_bin
此外print> print(os.path.abspath('。'。'))
在CGI中将正确的路径返回到脚本所在的位置。
但是,如果我希望CGI在系统print(OS.System('twopi -v')上报告现有二进制文件的版本,
仅返回0
。同样的事情发生打印(OS.System('ls'))
。
如果我在此Virtualenv中安装了相同的python(通过服务器上的完整路径名调用)在命令行中,并且call call print> print(os.system('twopi -v'))
,它正确地告诉我:twopi -GraphViz版本4.0.0(20220529.0937)
,然后在下一行:0
。同样,如果我执行print(OS.System('ls'))
对于CGI脚本而言,这种系统命令是否不可能?有什么方法可以迫使脚本报告这种系统命令的结果?
I'm building a cgi script with python on my web host and using wget to test it. The shebang #!/home/me/virtualenv/public_html/chartex/3.6/bin/python3.6
points it at the right version of python to use so that print(sys.version, sys.executable)
in the cgi correctly returns:
3.6.15 (default, Jan 14 2022, 12:16:54)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] /home/me/virtualenv/public_html/chartex/3.6/bin/python3.6_bin
Moreover print(os.path.abspath('.'))
in the cgi returns the correct path to where the script resides.
However, if I want the cgi to report the version of an existing binary on the system print(os.system('twopi -V'))
returns only 0
. The same thing happens doing print(os.system('ls'))
.
If I run the same python installed in this virtualenv (calling it by its full pathname on the server) at the command line, and there call print(os.system('twopi -V'))
, it correctly tells me: twopi - graphviz version 4.0.0 (20220529.0937)
and then on the next line: 0
. Likewise if I do print(os.system('ls'))
Is this kind of system command impossible for a cgi script? Is there a way I can coerce the script to report the result of such a system command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果任何人同样对
subprocess
同样感到困惑:多亏了@furas的观察结果,并且与
subprocess
杂乱无章,我终于找到了一种咒语,这会强迫我的python2 .7 CGI脚本给我命令twopi -v
的外壳输出。 @tripleee中的评论此线程是关键。对于Python 3.7.9及以上:“capture_output
是选项组合stdout = sudcrocess.pipe.pipe,stderr = subprocess.pipe.pipe
实际shell输出将用于<<<<代码> stderr
subprocess
模块是拜占庭的!In case anyone is similarly confused about
subprocess
:Thanks to the observations of @furas and a great deal of messing about with
subprocess
I finally figured out an incantation that would compel my python2.7 cgi script to give me the shell output of the commandtwopi -V
. A comment from @tripleee in this thread was the key. For Python 3.7.9 and up: "capture_output
is a shorthand for the option combinationstdout=supprocess.PIPE, stderr=subprocess.PIPE
The actual shell output was going to
stderr
. Thesubprocess
module is byzantine! It's a little better under python3, but not much.