2
votes

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?

2
Well, if you gave your source code... >_> - Oleh Prypin
Please stop shooting yourself in the foot, nobody does python for the web as CGI nowadays, you'll even be seen as the "weird" guy if you try to use mod_python for Apache. Please use a WSGI framework, there's many of them to choose from. Bottle, Flask or web.py can be a good start. - user1542167

2 Answers

1
votes

Looks like you're not sending the headers properly.

Try if this simplest example script runs:

print('Content-Type: text/html; charset=utf-8\n')
print("Hello, World!")
1
votes

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.