Write the following code and save it in / usr / lib / cgi-bin
with the extension .cgi
.
import subprocess
print('Content-type: text/html„n')
print(subprocess.run('hostname'))
When I access this CGI script from a browser, I get a 500 Internal Server Error
.
In the Apache log,
malformed header from script
Is output.
subprocess.run ()
does not output the result to standard output unless anything is specified.
By default, it does not capture standard output or standard error output. Pass PIPE in the stdout or / and stderr arguments if you want to capture.
https://docs.python.org/ja/3.5/library/subprocess.html
If subprocess.PIPE
is specified in the argument stdout
, the result will be output to standard output.
When using `subprocess.run ()'in a CGI script, do as follows.
result = subprocess.run('hostname', stdout=subprocess.PIPE)
print(result.stdout)
Recommended Posts