python - 使用 Python 的 CGI 脚本 - 在本地服务器上测试浏览器

标签 python windows command-line cgi

我正在尝试在 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 内容,而是脚本的全部内容。有什么想法吗?

  1. 是的,我所有的文件都有可执行权限。
  2. 是的,我还尝试从命令行执行 python -m CGIHTTPServer.. 这成功启动了服务器,我的脚本也是如此。 :(

最佳答案

弄明白了——我在 CGI 脚本的开头缺少解释器行。

关于python - 使用 Python 的 CGI 脚本 - 在本地服务器上测试浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407657/

相关文章:

python - 替换列表中稍后将在变量中使用的单词

Python 请求 : download only if newer

python - 生成日志文件后如何将其写入CSV

python - 属性错误 : 'HTTPHeaderDict' object has no attribute 'get_all' while mapping the mappings in elasticsearch

MySQL 错误 1114 : The table is full

python - 按下过多按键后 pyHook + pythoncom 停止工作 [Python]

c# - 如何为 Windows 窗体应用程序创建安装程序和卸载程序

apache - 如何从 Mac 上的命令行卸载 apache?

linux - 使用 exec 执行命令并将进程 ID 传递给另一个命令

windows - 如何将命令行上的双引号转义为 .vbs 脚本?