带有apache2的mod_python,(重新)导入模块错误

时间:2022-10-06 07:58:53

I'm trying to get mod-python to work with apache2 but not having any success. I've followed a few tutorials for getting mod-python working but I can't see what I'm doing wrong.

我试图让mod-python与apache2一起工作,但没有取得任何成功。我已经按照一些教程来获得mod-python的工作,但我看不出我做错了什么。

When I visit http://site.example.com/cgi-bin/test.py I actually get my 404 page! (whereas I get a 403 forbidden if the file really doesn't exist)

当我访问http://site.example.com/cgi-bin/test.py时,我实际上得到了我的404页面! (如果文件确实不存在,我会被禁止403)

Here's my setup:

这是我的设置:

In /etc/apache2/sites-enabled/ there are configuration files named after each of my domains. In the site.example.com file I've added this to the user directives section:

在/ etc / apache2 / sites-enabled /中有以我的每个域命名的配置文件。在site.example.com文件中,我已将其添加到用户指令部分:

# Begin user directives <--
<Directory /home/default/site.example.com/user/htdocs/cgi-bin/>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride AuthConfig
     Order allow,deny
     allow from all
     AddHandler mod_python .py
     PythonHandler mod_python.publisher
     PythonDebug On
</Directory>
# --> End user directives

full file here, which includes the line:

这里有完整的文件,其中包括以下内容:

EDIT to add contents of test.py:

编辑以添加test.py的内容:

#!/usr/bin/python  
print "Content-type: text/html"  
print  
print 'hello world'  
#print 1/0

As shown above, I get a 404 /python/test.py was not found on this server.

如上所示,我在此服务器上找不到404 /python/test.py。

But if I uncomment the last line I get:

但如果我取消注释我得到的最后一行:

Mod_python error: "PythonHandler mod_python.publisher"

Traceback (most recent call last):

  File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
    result = object(req)

  File "/usr/lib/python2.4/site-packages/mod_python/publisher.py", line 98, in handler
    path=[path])

  File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 457, in import_module
    module = imp.load_module(mname, f, p, d)

  File "/home/default/site.example.co.uk/user/htdocs/python/test.py", line 5, in ?
    print 1/0

ZeroDivisionError: integer division or modulo by zero

Does this traceback look right? Enabling cgitb has no effect by the way.

这个追溯看起来是否正确?顺便说一句,启用cgitb没有任何影响。

Is there any other info I can add to help diagnose this?

我可以添加任何其他信息来帮助诊断吗?

3 个解决方案

#1


As far as I know, the Publisher handler needs to take in a request object and a method in order to return the response.

据我所知,Publisher处理程序需要接受请求对象和方法才能返回响应。

In my scripts, I have something like this:

在我的脚本中,我有这样的事情:

#!/usr/bin/python  
def run(req):
  return 'Hello world!'

Then, go to http://.../python/test.py/run

然后,转到http://.../python/test.py/run

In this case, req is the request object that gets passed in by the Publisher Handler.

在这种情况下,req是由Publisher Handler传入的请求对象。

And, in case you don't have one already, you need to have an __init__.py file in the same directory where test.py is.

并且,如果您还没有,则需要在test.py所在的同一目录中包含__init__.py文件。

Also, with the Publisher, I noticed that sometimes, the response gets cached. I will get the same errors back in the browser that I had already fixed. So, it's not a bad idea to restart apache if you see an error that you think you fixed already.

此外,对于发布者,我注意到有时,响应会被缓存。我将在已修复的浏览器中返回相同的错误。因此,如果您发现您认为已经修复的错误,重启apache并不是一个坏主意。

#2


For starters, I would suggest not using the cgi-bin directory because that's intended for CGI scripts. Python files that get loaded with mod_python are not CGI scripts. (Theoretically you should be able to load Python files from the cgi-bin directory, but it'll be easier to debug if you give your Python files their own directory.)

对于初学者,我建议不要使用cgi-bin目录,因为这是用于CGI脚本的。使用mod_python加载的Python文件不是CGI脚本。 (从理论上讲,你应该能够从cgi-bin目录加载Python文件,但如果你将Python文件作为自己的目录,它将更容易调试。)

Now, your document root is /home/default/site.example.com/user/htdocs so I'd suggest creating a subdirectory of that, say /home/default/site.example.com/user/htdocs/python, to put your Python files in - just for testing. Don't create any Alias or ScriptAlias directives for this directory (because it's under the document root already), but you'll need to put something like this in your virtual host file:

现在,您的文档根目录是/home/default/site.example.com/user/htdocs所以我建议创建一个子目录,比如/home/default/site.example.com/user/htdocs/python,将您的Python文件放入 - 仅用于测试。不要为此目录创建任何Alias或ScriptAlias指令(因为它已经在文档根目录下),但是您需要在虚拟主机文件中放置这样的内容:

<Directory /home/default/site.example.com/user/htdocs/python/>
     Order allow,deny
     Allow from all
     AddHandler mod_python .py
     PythonHandler mod_python.publisher
     PythonDebug On
</Directory>

.Move the file test.py to that directory and try going to http://site.example.com/python/test.py, and you should see the results of the index function in the file test.py.

将文件test.py移动到该目录并尝试访问http://site.example.com/python/test.py,您应该在文件test.py中看到索引函数的结果。

#3


The python file should look like this in order to get the desired output:

python文件应如下所示,以获得所需的输出:

def index():
  return "hello world"

"http://somesite.com/path/test.py" opens the function "index" by default.

“http://somesite.com/path/test.py”默认打开函数“index”。

"http://somesite.com/path/test.py/run" opens the function "run" instead.

“http://somesite.com/path/test.py/run”会打开“运行”功能。

You can get fancier, but the above will give you a basic mod_python page.

你可以得到更好的,但上面会给你一个基本的mod_python页面。

#1


As far as I know, the Publisher handler needs to take in a request object and a method in order to return the response.

据我所知,Publisher处理程序需要接受请求对象和方法才能返回响应。

In my scripts, I have something like this:

在我的脚本中,我有这样的事情:

#!/usr/bin/python  
def run(req):
  return 'Hello world!'

Then, go to http://.../python/test.py/run

然后,转到http://.../python/test.py/run

In this case, req is the request object that gets passed in by the Publisher Handler.

在这种情况下,req是由Publisher Handler传入的请求对象。

And, in case you don't have one already, you need to have an __init__.py file in the same directory where test.py is.

并且,如果您还没有,则需要在test.py所在的同一目录中包含__init__.py文件。

Also, with the Publisher, I noticed that sometimes, the response gets cached. I will get the same errors back in the browser that I had already fixed. So, it's not a bad idea to restart apache if you see an error that you think you fixed already.

此外,对于发布者,我注意到有时,响应会被缓存。我将在已修复的浏览器中返回相同的错误。因此,如果您发现您认为已经修复的错误,重启apache并不是一个坏主意。

#2


For starters, I would suggest not using the cgi-bin directory because that's intended for CGI scripts. Python files that get loaded with mod_python are not CGI scripts. (Theoretically you should be able to load Python files from the cgi-bin directory, but it'll be easier to debug if you give your Python files their own directory.)

对于初学者,我建议不要使用cgi-bin目录,因为这是用于CGI脚本的。使用mod_python加载的Python文件不是CGI脚本。 (从理论上讲,你应该能够从cgi-bin目录加载Python文件,但如果你将Python文件作为自己的目录,它将更容易调试。)

Now, your document root is /home/default/site.example.com/user/htdocs so I'd suggest creating a subdirectory of that, say /home/default/site.example.com/user/htdocs/python, to put your Python files in - just for testing. Don't create any Alias or ScriptAlias directives for this directory (because it's under the document root already), but you'll need to put something like this in your virtual host file:

现在,您的文档根目录是/home/default/site.example.com/user/htdocs所以我建议创建一个子目录,比如/home/default/site.example.com/user/htdocs/python,将您的Python文件放入 - 仅用于测试。不要为此目录创建任何Alias或ScriptAlias指令(因为它已经在文档根目录下),但是您需要在虚拟主机文件中放置这样的内容:

<Directory /home/default/site.example.com/user/htdocs/python/>
     Order allow,deny
     Allow from all
     AddHandler mod_python .py
     PythonHandler mod_python.publisher
     PythonDebug On
</Directory>

.Move the file test.py to that directory and try going to http://site.example.com/python/test.py, and you should see the results of the index function in the file test.py.

将文件test.py移动到该目录并尝试访问http://site.example.com/python/test.py,您应该在文件test.py中看到索引函数的结果。

#3


The python file should look like this in order to get the desired output:

python文件应如下所示,以获得所需的输出:

def index():
  return "hello world"

"http://somesite.com/path/test.py" opens the function "index" by default.

“http://somesite.com/path/test.py”默认打开函数“index”。

"http://somesite.com/path/test.py/run" opens the function "run" instead.

“http://somesite.com/path/test.py/run”会打开“运行”功能。

You can get fancier, but the above will give you a basic mod_python page.

你可以得到更好的,但上面会给你一个基本的mod_python页面。