【发布时间】:2013-12-05 18:19:46
【问题描述】:
我正在尝试在 Windows 机器上本地测试 CGI Python 脚本。
我遵循了一个教程,所以它非常简单。这是我的 Python 脚本(名为 test_cgi.py,位于 cgi-bin 目录中)。
print "Content-type: text/html"
print "<html>"
print "<title>Test CGI</title>"
print "<body>"
print "<p>Hello World!</p>"
print "</body>"
print "</html>"
这是另一个启动服务器的脚本:
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = [""]
httpd = server(server_address, handler)
httpd.serve_forever()
启动服务器后,当我导航到http://localhost:8000/test_cgi.py 时,浏览器不会显示 HTML 内容,而是显示脚本的整个内容。有什么想法吗?
- 是的,我所有的文件都有可执行权限。
- 是的,我也尝试从命令行执行 python -m CGIHTTPServer。这成功启动了服务器,我的脚本也是如此。 :(
【问题讨论】:
-
Aside:您的脚本有错误。您需要在“Content-type”行之后打印一个空行。
标签: python windows command-line cgi