在Python CGI脚本中使用OS.System()

发布于 2025-02-06 14:21:18 字数 870 浏览 4 评论 0原文

我正在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2025-02-13 14:21:18

如果任何人同样对subprocess同样感到困惑:

多亏了@furas的观察结果,并且与subprocess杂乱无章,我终于找到了一种咒语,这会强迫我的python2 .7 CGI脚本给我命令twopi -v的外壳输出。 @tripleee中的评论此线程是关键。对于Python 3.7.9及以上:“ capture_output是选项组合stdout = sudcrocess.pipe.pipe,stderr = subprocess.pipe.pipe

p = subprocess.Popen(['twopi', '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("stderr:  " + p.stderr.read())
print("stdout:  " + p.stdout.read())
# with the result:
stderr:  twopi - graphviz version 4.0.0 (20220529.0937)
stdout:  

实际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 command twopi -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 combination stdout=supprocess.PIPE, stderr=subprocess.PIPE

p = subprocess.Popen(['twopi', '-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("stderr:  " + p.stderr.read())
print("stdout:  " + p.stdout.read())
# with the result:
stderr:  twopi - graphviz version 4.0.0 (20220529.0937)
stdout:  

The actual shell output was going to stderr. The subprocess module is byzantine! It's a little better under python3, but not much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文