malformed header from script index.py Bad header

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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 

回答1:

You should end your header with \r\n, then you must print out yet another \r\n to signal that the body is coming.

(In other words, it's interpreting your body as a Header because the headers were never terminated)



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!