I put a simple python script inside the cgi-bin in apache2 and tried to execute it using the browser as follows,
"http://www.example.com/cgi-bin/test.py"
But it gives a 500 Internal sever error.
Following is the error.log in apache2.
[Sun Jul 29 22:07:51 2012] [error] (8)Exec format error: exec of '/usr/lib/cgi-bin/test.py' failed
[Sun Jul 29 22:07:51 2012] [error] [client ::1] Premature end of script headers: test.py
[Sun Jul 29 22:07:51 2012] [error] [client ::1] File does not exist: /var/www/favicon.ico
can anyone help me on this?
BlaXpirit's answer should solve your problem with a 500 server internal error.
It is important to note the "\n" at the end of the first print statement.  You can also write it as 
print("Content-Type: text/html; charset=utf-8")
print()
I was surprised to learn that writing out these headers is necessary even if your Python program is only going to do server-side work - with no response to the browser at all.
How to run a python script inside the cgi-bin of apache server?