gpt4 book ai didi

未呈现的 Python 页面

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:03 25 4
gpt4 key购买 nike

我创建了一个工作正常的 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 文件呈现良好,即使将文件移回服务器运行的根文件夹也没有区别,我尝试以 root 和其他普通用户身份运行, 结果完全一样。

尝试谷歌搜索“未呈现 python 页面”但没有发现任何有用的东西!

编辑

我也试过运行一个没有覆盖的更简单的服务器,但结果是相同的,pything 代码永远不会呈现:

pyserv.py

#!/usr/bin/python
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
serve = HTTPServer(("",80),CGIHTTPRequestHandler)
serve.serve_forever()

最佳答案

我相信您遇到这个问题是因为您覆盖了 cgi_directories

documentation的相关部分阅读:

“这默认为 ['/cgi-bin', '/htbin'] 并描述包含 CGI 脚本的目录。”

要么将您的脚本放在根目录中,要么删除对 cgi_directories 的覆盖并将脚本放在 /cgi-bin 目录中。

这是一个很好的链接,它逐行描述了类似的简单设置: https://pointlessprogramming.wordpress.com/2011/02/13/python-cgi-tutorial-1/

更新:

根据 comment在上面的页面上,似乎设置 cgi_directories = [""] 会导致 禁用 cgi 目录功能。相反,设置 cgi_directories = ["/"],将其设置为当前目录。

关于未呈现的 Python 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14679747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com