Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

run python script as cgi apache server

I am trying to make a python script run as cgi, using an Apache server. My script looks something like this:

  #!/usr/bin/python
  import cgi
  if __name__ == "__main__":

  print("Content-type: text/html")
  print("<HTML>")
  print("<HEAD>")

I have done the necessary configurations in httpd.conf(in my opinion):

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

I have set the execution permission for the script with chmod

However, when I try to access the script via localhost i get an Error 500:End of script output before headers:script.py What could be the problem? The script is created in an Unix like environment so I think the problem of clrf vs lf doesn't stand. Thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you are missing a print statement after

print("Content-type: text/html")

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.

The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc.

It may look like

#!/usr/bin/env python

print "Content-Type: text/html"
print
print """
    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
    Hello, world!
"""

For more details visit python-cgi

For python3

#!/usr/bin/env python3

print("Content-Type: text/html")
print()
print ("""
    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
    Hello, world!
"""
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...