我有以下用于 CGI 处理 HTTP 服务器的最小代码,这些代码源自内管上的几个示例:
#!/usr/bin/env python
import BaseHTTPServer
import CGIHTTPServer
import cgitb;
cgitb.enable() # Error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = [""]
httpd = server(server_address, handler)
httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud)
然而,当我执行脚本并尝试使用 CGI 在同一目录中运行测试脚本时http://localhost:8000/test.py,我看到的是脚本的文本,而不是执行的结果。
权限都设置正确,测试脚本本身不是问题(因为python -m CGIHTTPServer当脚本驻留在 cgi-bin 中时,我可以使用 运行它)。我怀疑这个问题与默认的 CGI 目录有关。
我怎样才能让脚本执行?
我正在 Windows 上编写一个简单的 python Web 服务器..
它可以工作,但现在我想运行动态脚本(php 或 py)而不仅仅是 html 页面..
这是我的代码:
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler
class RequestsHandler(CGIHTTPRequestHandler):
cgi_directories = ["/www"] #to run all scripts in '/www' folder
def do_GET(self):
try:
f = open(curdir + sep + '/www' + self.path)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f.read())
f.close()
except IOError:
self.send_error(404, "Page '%s' not found" % self.path)
def main():
try:
server = HTTPServer(('', 80), RequestsHandler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
如果我将 php 代码放在 www …