Use Python3's Subprocess.run () in a CGI script

phenomenon

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.

Cause and solution

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

Use Python3's Subprocess.run () in a CGI script
A memorandum that you will often use in Python's Selenium
Run a multi-line script in a PDB
Use print in a Python2 lambda expression
Use a scikit-learn model trained in PySpark
Use Django from a local Python script
Let's run a Bash script in Java
Use WebDAV in a Portable Docker environment
Use the latest pip in a virtualenv environment
Call a Python script from Embedded Python in C ++ / C ++
Use a custom error page in python / tornado
Use a free GPU in your favorite environment
I made a script to put a snippet in README.md
A memorandum on how to use keras.preprocessing.image in Keras
What's in that variable (when running a Python script)
Convenient to use matplotlib subplots in a for statement
Use communicate () when receiving output in a Python subprocess
Use Sudachipy's learned word2vec in a low memory environment
Use config.ini in Python
Use DataFrame in Java
Use Mean in DataFrame
Use Valgrind in Python
Use a Property Decorator?
dict in dict Makes a dict a dict
Use ujson in requests
Use profiler in Python
How to use the __call__ method in a Python class
Let's create a script that registers with Ideone.com in Python.
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
Use MeCab to translate sloppy sentences in a "slow" way.
Process the files in the folder in order with a shell script
I wrote a script to get a popular site in Japan
Execute a command in Go like Python's subprocess.call (~, shell = True)
A set of script files that do wordcloud in Python3
I wrote a script that splits the image in two
Should I use this to refer to my class in a static method like Python's @classmethod in ES2015?