Python简单CGI服务器os.execve(scriptfile,args,os.environ)权限错误
01 January 2010
这段时间在学Python,今天开始进入到CGI编程,示例中有个简单CGI Server的例子,代码如下:
webserver.py:
webdir = "." #html files directory
port = 8000 #server port
import os, sys
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
if sys.platform[:3] == "win":
CGIHTTPRequestHandler.have_popen2 = False
CGIHTTPRequestHandler.have_popen3 = False
os.chdir(webdir)
srvraddr = ("", port)
srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler)
srvrobj.serve_forever()
页面CGI为cgi101.py:
#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print "Content-type: text/html/n"
print "<title>Reply Page</title>"
if not form.has_key("username"):
print "<h1>Who are you?</h1>"
else:
print "<h1>Hello <i>%s</i>!</h1>" % cgi.escape(form['username'].value)
HTML页面为:
<html>
<title>Interactive Page</title>
<body>
<form method="post" action="cgi-bin/cgi101.py">
<p><b>Enter your name:</b></p>
<p>Username:<input type="text" name="username"/></p>
<p>Password:<input type="password" name="pwd"/></p>
<input type="submit" name="submit" text="submit"/>
</form>
</html>
先运行webserver.py(我是在Ubuntu上,用sudo python webserver.py)启动了server,然后在Firefox中访问:http://localhost:8000/cgi101.html
结果Firefox中弹出下载cgi101.py脚本的对话框,而不是运行cgi返回结果,server终端中显示:
localhost - - [01/Jan/2010 17:05:32] "GET /cgi-bin/cgi101.py?username=Jone HTTP/1.1" 200 -
Traceback (most recent call last):
File "/usr/lib/python2.6/CGIHTTPServer.py", line 255, in run_cgi
os.execve(scriptfile, args, os.environ)
OSError: [Errno 13] Permission denied
说明os.execve权限出问题了,解决方法如下,将cgi101.py的权限设为777,如果还是不行的话,要将整个cgi目录的权限设为777(例如,本例中使用命令chmod -R 777 cgi-bin)
然后就可以了。
Good Luck!
PS: Happy New Year !
blog comments powered by Disqus