我已经创建了一个工作良好的CGIHTTPServer,问题是无论我做什么,python页面都不会呈现,源代码总是显示在浏览器中。
pyhttpd.py
#!/usr/bin/python
import CGIHTTPServer
import BaseHTTPServer
class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
cgi_directories = [""]
PORT = 8080
httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()cgi-bin/hello.py
#!/usr/bin/python
print 'Content-Type: text/html'
print
print '<html>'
print '<head><title>Hello</title></head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body></html>'http://some.ip.address:8080/cgi-bin/hello.py
#!/usr/bin/python
print 'Content-Type: text/html'
print
print '<html>'
print '<head><title>Hello</title></head>'
print '<body>'
print '<h2>Hello World</h2>'
print '</body></html>'我已将所有文件的权限设置为可执行文件,.html文件呈现良好,甚至将文件移回运行服务器的根文件夹也没有什么区别,我尝试作为根用户以及其他普通用户运行,结果完全相同。
尝试谷歌"python页面没有呈现“,但没有发现任何有用的东西!
编辑
我还尝试运行一个没有重写的简单服务器,但结果是相同的,编写代码从来不呈现:
pyserv.py
#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()发布于 2013-02-04 02:58:47
我相信你有这个问题是因为你已经超越了cgi_directories。
文档的相关部分如下:
“这默认为['/cgi-bin', '/htbin'],并描述了要处理为包含CGI脚本的目录。”
要么将脚本放在根目录中,要么删除对cgi_directories的重写,并将脚本放在/cgi-bin目录中。
下面是一个很好的链接,描述了类似的简单设置:https://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/
更新:
根据上面页面上的评论,设置cgi_directories = [""]似乎会导致禁用cgi目录功能。相反,设置cgi_directories = ["/"],将其设置为当前目录。
https://stackoverflow.com/questions/14679747
复制相似问题