简体   繁体   中英

run python scripts on apache (linux and windows)

I need help with how to run scripts test.cgi for example in apache ? I created test script:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!

When I run localhost/cgi-bin/test.cgi

It gives me error.

Internal Server Error

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

Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.2.22 (Ubuntu) Server at localhost Port 80

Python is installed properly. Test.cgi chmod is 755

I am new to linux. On wndows it gives me error too . On windows it gives me:

End of script output before headers: python_test.cgi

I use this script on windows:

#!"C:\Python34\python.exe"
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!"

Any idea ?

Add these to your configuration file:

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

and replace your code with:

import cgitb
cgitb.enable()

print ("Content-Type: text/plain;charset=utf-8")
print ()

print ("Hello World!")

It worked for me. In Python 3.x print is a function, so you have to pass the string to be printed as an arguement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM