我正在尝试在 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.. 这成功启动了服务器,我的脚本也是如此。 :(
最佳答案
弄明白了——我在 CGI 脚本的开头缺少解释器行。
关于python - 使用 Python 的 CGI 脚本 - 在本地服务器上测试浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407657/