Python CGIHTTPServer默认目录 [英] Python CGIHTTPServer Default Directories

查看:256
本文介绍了Python CGIHTTPServer默认目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于CGI处理HTTP服务器,我有以下最少的代码,这些代码是从内管上的几个示例得出的:

I've got the following minimal code for a CGI-handling HTTP server, derived from several examples on the inner-tubes:

#!/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()

但是,当我执行脚本并尝试使用 http:// localhost:8000通过CGI在同一目录中运行测试脚本时, /test.py ,我看到脚本的文本而不是执行的结果。

Yet, when I execute the script and try to run a test script in the same directory via CGI using http://localhost:8000/test.py, I see the text of the script rather than the results of the execution.

所有权限都已正确设置,并且测试脚本本身不是问题(因为我可以使用 python -m CGIHTTPServer正常运行(当脚本位于cgi-bin中时)。我怀疑问题与默认的CGI目录有关。

Permissions are all set correctly, and the test script itself is not the problem (as I can run it fine using python -m CGIHTTPServer, when the script resides in cgi-bin). I suspect the problem has something to do with the default CGI directories.

如何获取执行脚本的脚本?

推荐答案

我的猜想是正确的。派生该代码的示例显示了将默认目录设置为服务器脚本所在目录的错误方法。要以这种方式设置默认目录,请使用:

My suspicions were correct. The examples from which this code is derived showed the wrong way to set the default directory to be the same directory in which the server script resides. To set the default directory in this way, use:

handler.cgi_directories = ["/"]

警告:如果您不使用任何类型的防火墙,这将打开潜在的巨大安全漏洞。这仅是一个示例。仅在格外小心的情况下使用。

这篇关于Python CGIHTTPServer默认目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆