【问题标题】:run python scripts on apache (linux and windows)在 apache(linux 和 windows)上运行 python 脚本
【发布时间】:2015-02-18 06:48:18
【问题描述】:

我需要有关如何在 apache 中运行脚本 test.cgi 的帮助? 我创建了测试脚本:

#!/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!

当我运行localhost/cgi-bin/test.cgi

它给了我错误。

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 已正确安装。 Test.cgi chmod 是 755

我是 linux 新手。在 wndows 上它也给了我错误。 在 Windows 上它给了我:

End of script output before headers: python_test.cgi

我在 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!"

有什么想法吗?

【问题讨论】:

  • 能否请您在httpd.conf 文件中显示配置?
  • 在 linux 中是空的,windows 是好的,因为 perl 脚本工作正常。我添加了 LoadModule cgi_module modules/mod_cgi.so 和 AddHandler cgi-script .cgi .pl .asp .py

标签: python linux windows apache


【解决方案1】:

将这些添加到您的配置文件中:

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

并将您的代码替换为:

import cgitb
cgitb.enable()

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

print ("Hello World!")

它对我有用。在 Python 3.x 中,print 是一个函数,因此您必须将要打印的字符串作为参数传递。

【讨论】:

  • 在 Windows 上?我在 windows 上使用 xampp,在 linux 上只使用 apache。
  • 是的,xampp 中会有一个httpd.conf 文件
  • 我知道,我之前编辑过它以启用 cgi_mod。我对它进行了处理,但它没有用。我用的是python3.4会不会出现这个错误?
猜你喜欢
  • 2017-05-22
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 2022-08-06
  • 2019-09-11
  • 2016-03-10
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多