run python scripts on apache (linux and windows)

孤街浪徒 提交于 2021-02-20 04:10:41

问题


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 runlocalhost/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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/27564253/run-python-scripts-on-apache-linux-and-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!