Python CGI os.system causing malformed header
<p>I am running Apache/2.4.10 (Raspbian) and I am using python for CGI.
But when I try to use os.system in simple code I get this malformed header error:</p>

<pre><code>[Wed Aug 31 17:10:05.715740 2016] [cgid:error] [pid 3103:tid 1929376816] [client 192.168.0.106:59277] malformed header from script'play.cgi': Bad header: code.cgi
</code></pre>

<p>Here is the code from play.cgi:</p>

<pre><code>#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cgi
import os

print('Content-type: text/html')
print('')

os.system('ls')
</code></pre>

<p>The strange thing is that if I remove the os.system line it mysteriously starts working again. I have tried using popen instead, same problem. I have tried to obscure it in some code,change file name, different encodings and even time.sleep, none of those worked.</p>

<p>The strangest thing is that it works perfectly fine in a more complicated code.</p>

<p>To see why the problem happened, try launching your script</p>

<pre><code>python webls.py &gt; output
</code></pre>

<p>And than open <code>output</code> with some text editor. You will notice that your <code>Content-type: text/html</code> ended up being in the bottom of the file, which, of course, is wrong.</p>

<p>That happens because incorporating outout of your <code>os.system</code> in output of Python code screws thing up (because think about it: your <code>print(...)</code> is accumulated and flushed in blocks when appropriate, while <code>os.system()</code> abruptively prints data and because it is <code>os.system</code> transacton, flushes only its result (which is also why you do not see the problem if outout is to console)). The solution is to flush output after printing headers. You should change your code to</p>

<pre><code>#!/usr/bin/python

import cgi
import os
import sys

print 'Content-type: text/html'
print ''

sys.stdout.flush()

os.system('ls')
</code></pre>

<hr>

<p>Although that is a fix, you know you are doing something terribly wrong if you need to incorporate output of your console command to the content of the web page and use <code>os.system</code> for that. There are several things you should consider doing. These solutions are sorted by how recommended they are (from crappy to good):</p>

<p>-Use redirection of input/output. Save output of <code>ls</code> to file and read it in your Python code:</p>

<pre><code>os.system('ls &gt; /tmp/lsoutput')
print open('/tmp/lsoutput', 'r').read()
</code></pre>

<p>-Use subprocess. It allows you to capture output of console program and use it in your python code (example from <a href="http://stackoverflow.com/questions/6657690/python-getoutput-equivalent-in-subprocess">python getoutput() equivalent in subprocess</a>)</p>

<pre><code>import subprocess
process = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE)
out, err = process.communicate()
print(out)
</code></pre>

<p>-Do not call external programs at all, this is a bad thing to do. If you need a list of files, use Python functions instead</p>

<pre><code>import os

for filename in os.listdir('.'):
    print filename
</code></pre>

python apache cgi