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 !

Deep 3015
  • 9,929
  • 5
  • 30
  • 54
  • 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" – Deep 3015 May 16 '16 at 07:59
  • In the Python script deployed on the server, is there a line break between the 2nd and 3rd print statement? – Maximilian Peters May 16 '16 at 09:59

1 Answers1

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:

Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • 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 – Deep 3015 May 17 '16 at 06:27
  • 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? – Maximilian Peters May 17 '16 at 06:33
  • And get ideas from site https://www.linux.com/blog/configuring-apache2-run-python-scripts – Deep 3015 May 17 '16 at 06:38
  • Can you post your http.conf entry? – Maximilian Peters May 17 '16 at 06:38
  • ServerAdmin webmaster@localhost DocumentRoot /var/www/html Options FollowSymLinks AllowOverride None Options +ExecCGI AddHandler cgi-script .cgi .py Order allow,deny Allow from all ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all – Deep 3015 May 17 '16 at 06:43
  • config file is 000-default.conf(/etc/apache2/sites-available) – Deep 3015 May 17 '16 at 06:45
  • owner of your python script is www-data? – Maximilian Peters May 17 '16 at 06:50
  • finally i got clue from first option of http://stackoverflow.com/a/7914384/3898339 – Deep 3015 May 17 '16 at 07:27
  • is your script executable? i.e. can you call it from the command line? – Maximilian Peters May 17 '16 at 07:38