Skip to content Skip to sidebar Skip to footer

Python Cgi Os.system Causing Malformed Header

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: [Wed Aug 31 17:10:05.715740 2

Solution 1:

To see why the problem happened, try launching your script

python webls.py > output

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

That happens because incorporating outout of your os.system in output of Python code screws thing up (because think about it: your print(...) is accumulated and flushed in blocks when appropriate, while os.system() abruptively prints data and because it is os.system 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

#!/usr/bin/python

import cgi
import os
import sys

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

sys.stdout.flush()

os.system('ls')

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 os.system for that. There are several things you should consider doing. These solutions are sorted by how recommended they are (from crappy to good):

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

os.system('ls > /tmp/lsoutput')
printopen('/tmp/lsoutput', 'r').read()

-Use subprocess. It allows you to capture output of console program and use it in your python code (example from python getoutput() equivalent in subprocess)

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

-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

import osfor filename inos.listdir('.'):
    print filename

Post a Comment for "Python Cgi Os.system Causing Malformed Header"