1

I am not able to run python cgi script in ubuntu using xampp

I have made necessary changes in httpd.conf(/opt/lampp/etc)

<Directory "/opt/lampp/htdocs">
  Options +ExecCGI
  AddHandler cgi-script .cgi .py
  Order allow,deny
  Allow from all
</Directory>

==========================================================

and my python script file is

#!/usr/bin/python
# -*- coding: UTF-8 -*-# enable debugging
import cgitb
cgitb.enable()    
print "Content-Type: text/html;charset=utf-8"
print "Hello World!"

===========================================================

Thanks in advance !

2
  • After I run , get error as "The server encountered an internal error and was unable to complete your request. Error message: End of script output before headers: labels.py" Commented May 16, 2016 at 7:59
  • In the Python script deployed on the server, is there a line break between the 2nd and 3rd print statement? Commented May 16, 2016 at 9:59

1 Answer 1

1

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following.

https://docs.python.org/2/library/cgi.html

In your current code example the blank line is missing (not in your originally posted code). Try changing the first print statement to:

print "Content-Type: text/html;charset=utf-8\n"

and it should work (at least it did on a Amazon server with Ubuntu and Apache).

My personal troubleshooting workflow with Python and cgi:

Sign up to request clarification or add additional context in comments.

9 Comments

I get error in error.log as [Tue May 17 11:54:24.307673 2016] [cgid:error] [pid 15122:tid 140289761994624] (2)No such file or directory: AH01241: exec of '/var/www/html/test/labels.py' failed [Tue May 17 11:54:24.307933 2016] [cgid:error] [pid 15020:tid 140289461118720] [client 127.0.0.1:60209] End of script output before headers: labels.py
In your httpd.conf your cgi directory is "/opt/lampp/htdocs" but the Python script seems to be in '/var/www/html/test/labels.py'. Does it work when you move the script or change http.conf?
Can you post your http.conf entry?
ServerAdmin webmaster@localhost DocumentRoot /var/www/html <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/html/test> Options +ExecCGI AddHandler cgi-script .cgi .py Order allow,deny Allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.