internal server error (500) in simple cgi script

2019-04-23 18:39发布

I am trying to run a simple cgi script after configuring my server.

My script looks like this:

print "Content-type: text/html"
print
print "<html><head><title>CGI</title></head>"
print "<body>"
print "hello cgi"
print "</body>"
print "</html>"

When I go to my scripts url http://127.0.0.1/~flybywire/cgi-bin/main.py I get:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

And in error.log I get the following:

[error] (8)Exec format error: exec of '/home/flybywire/www/cgi-bin/main.py' failed [error] [client 127.0.0.1] Premature end of script headers: main.py

Other info: Apache/2.2.8 (Ubuntu) DAV/2 SVN/1.4.6 Server at 127.0.0.1 Port 80

6条回答
干净又极端
2楼-- · 2019-04-23 18:59

Also, save the file (if this is a Linux server) with Unix line endings. You did make it executable using chmod +x didn't you?

You can use #!/usr/bin/env python to cover the current running Python version if you're running in various environments (hence the env part).

查看更多
倾城 Initia
3楼-- · 2019-04-23 19:00

It looks like Apache has trouble executing it. Typically for a unix script you also need to specify the interpreter at the top of the script.

Try adding this to the top:

#!/usr/bin/python
查看更多
Melony?
4楼-- · 2019-04-23 19:02

Putting

#!/usr/bin/env python

on the top of the script works fine. I put it on top, but Netbeans was putting extra code (import commands) by itself on the top of the page which drove me crazy :(

查看更多
闹够了就滚
5楼-- · 2019-04-23 19:06

You might need a #!/usr/bin/python at the top of your script to tell Apache to use Python to execute it. At least, I did that and it worked for me :-) .

查看更多
三岁会撩人
6楼-- · 2019-04-23 19:06

Maybe your problem is that new python version needs parentheses ( ).

So your:

print "<body>"

Now should to be:

print ("<body>")
查看更多
▲ chillily
7楼-- · 2019-04-23 19:07

remove the 2nd line in your program (print) I tried it on my apache server (mac os x) it works fine. don't forget to chmod 755 and reboot with sudo apachectl restart This is for python 2.7

print "Content-type: text/html"
print "<html><head><title>CGI</title></head>"
print "<body>"
print "hello cgi"
print "</body>"
print "</html>"
查看更多
登录 后发表回答