Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malformed header from script index.py Bad header

Tags:

python

apache

I want to run python code in apache2(ubuntu 14.04) server. I have followed these steps and getting Error:

Step 1:

Configuration 1: I have created a directory and file under

/var/www/cgi-bin

Configuration 2 : I have edited /etc/apache2/sites-available/000-default.conf

Alias /cgi-bin /var/www/cgi-bin
<Directory "/var/www/cgi-bin">
Options Indexes FollowSymLinks ExecCGI
AddHandler cgi-script .cgi .py
Allow from all
</Directory>

<Directory /var/www/cgi-bin>
Options All
</Directory>

Step 2:

and my python script is: index.py

#!/usr/bin/env python
import cgi;
import cgitb;cgitb.enable()

print "Content-Type: text/plain\n"

print "<b>Hello python</b>"

step 3:

When i ran through chrome browser using: URL : http://localhost/cgi-bin/index.py

step 4:

I am getting this Error in error-log

malformed header from script 'index.py': Bad header: Hello Python
like image 202
Sahadev Avatar asked Jan 16 '16 08:01

Sahadev


1 Answers

Try this script

#!/usr/bin/env python
import cgi;
import cgitb;cgitb.enable()

print "Content-Type: text/html"
print "" #use this double quote print statement to add a blank line in the script
print "<b>Hello python</b>"

There should be one line space between header and main html content. That's why we have to use extra print statement before starting html tags in script.

like image 139
4 revs Avatar answered Sep 21 '22 13:09

4 revs