Python CGIHTTPServer崩溃,并显示"OSError:[Errno 13]权限被拒绝". [英] Python CGIHTTPServer crashes with "OSError: [Errno 13] Permission denied"

查看:261
本文介绍了Python CGIHTTPServer崩溃,并显示"OSError:[Errno 13]权限被拒绝".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在主目录中运行以下命令:

I am running the following command from my home directory:

python -m CGIHTTPServer

这将运行服务器,但是当我尝试访问cgi-bin目录中的脚本时,会得到:

This runs the server, but when I try to access a script in the cgi-bin directory I get:

Traceback (most recent call last):
  File "/usr/lib/python2.7/CGIHTTPServer.py", line 251, in run_cgi
    os.execve(scriptfile, args, env)
OSError: [Errno 13] Permission denied

以root身份运行不会产生任何影响.这些文件似乎具有所有正确的权限:

Running as root does not make a difference. The files seem to have all the right permissions:

student@bandersnatch:~$ ls -lhR
.:
total 12K
drwxr-xr-x 2 student student 4.0K Jun 13 18:38 cgi-bin
drwxr--r-- 2 student student 4.0K Jun 10  2004 kalpy
-rwxrwxrwx 1 student student 2.0K Jun 13 12:37 test.html

./cgi-bin:
total 8.0K
-rwxr-xr-x 1 student student 31 Jun 13 18:38 test.py

test.py的内容是:

The content of test.py is:

#!/usr/bin/python
print "test"

shebang有效:

The shebang is valid:

~$ which python
/usr/bin/python

推荐答案

您是否有机会以root用户身份运行该进程?

Are you, by any chance, running the process as root?

如果使用源,则在调用execve之前,您会在CGIHTTPServer.py中看到:

If you use the source, you will see in CGIHTTPServer.py, just before calling execve:

try:
    os.setuid(nobody)
except os.error:
    pass

也就是说,如果它能够更改UID(即它是root用户),它将以没有人的身份运行CGI脚本.如果不是root用户,则此调用很可能会失败并继续.

That is, it will run the CGI script as nobody, if it is able to change the UID, that is if it is root. If it is not root, this call will most likely fail, and pass on.

所以我的猜测是您以root用户身份运行服务器,因此脚本以 nobody 身份运行,但是该用户无权访问该脚本.正如您所说的那样,这在您的主目录中.

So my guess is that you are running the server as root, so the script is run as nobody, but this user doesn't have access to the script. Which is expected, as you say that it is in your home dir.

我能想到的两种解决方案:

Two solutions that I can think of:

  • 建议:不要以root用户身份运行服务器!
  • 解决方法:将脚本复制到任何人都无法读取的目录(例如,/tmp).

这篇关于Python CGIHTTPServer崩溃,并显示"OSError:[Errno 13]权限被拒绝".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆