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?
os.system()is the exit status of the program.0means that the program terminated without an error. Why do you think that it's a problem if it returns 0?os.system('twopi -V'), it doesn't also returntwopi - graphviz version 4.0.0 (20220529.0937)subprocessto catch displayed texttwopi - graphviz version 4.0.0 (20220529.0937)os.system()then it runs system shell which runs program and shell displays its result - not Python. Python displays only exit code -0.subprocess,run()or subprocess,check_output(). It runs program and it can catch text displayed in shell. And when you get this text then you can useprint()to display it on page.