【发布时间】:2012-06-13 13:21:30
【问题描述】:
我无法打开我的 python 文件。我是使用 python 的新手,我来自 PHP 背景,这对我来说完全不同。 我在服务器 `public_html/python/index.py' 上用一个简单的 hello world 代码创建了一个测试文件:
#!/usr/bin/python
print 'Content-type: text/html\r\n'
print '\r\n'
print 'Hello, World!'
我一直在环顾四周,显然我需要执行一些配置步骤才能运行 python 文件。
在 shell y 上,通过运行 python 确保 python 已安装并正常工作:
Python 2.4.3 (#1, Feb 22 2012, 16:05:45)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
然后我寻找安装python的正确路径type -p python,它是正确的:
user@domain.com [~]# type -p python
/usr/bin/python
最后我在一篇博客中读到,为了在 cgi-bin 文件夹之外执行 python 文件,我需要使用以下几行修改我的 .htaccess 文件:
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
这也不起作用,但即使我尝试在 cgi-bin 文件夹中运行它,我仍然会遇到同样的错误。
以防万一我也更改了文件的权限,什么都没有:
user@domain.com [~]# chmod a+x public_html/python/index.py
在所有这些之后,当我尝试在 Web 浏览器上打开 url 时,我仍然收到 500 内部服务器错误。
当我执行od index.py -c 时,我得到以下信息:
0000000 # ! / u s r / b i n / p y t h o
0000020 n \r \n \r \n p r i n t ' C o n t
0000040 e n t - t y p e : t e x t / h
0000060 t m l \ r \ n ' \r \n p r i n t
0000100 ' \ r \ n ' \r \n p r i n t ' H
0000120 e l l o , W o r l d ! '
0000135
感谢这位不起眼的 python 新手的帮助和指导。 顺便说一句,我在专用服务器中使用 WHM 和 CPanel。
【问题讨论】:
-
Python 不是 PHP:你不能在这样的 HTML 页面中内联代码。考虑改用网络框架。
-
od index.py -c的输出是什么? -
我将结果添加到我的问题中,谢谢!
标签: python apache server-configuration