在 Mac OS X 上使用 CGIHTTPServer 提供 Python 脚本服务

发布于 2024-09-13 04:17:34 字数 672 浏览 3 评论 0原文

我正在尝试在 Mac OS X 上设置 Python 的 CGIHTTPServer 以便能够在本地提供 CGI 脚本,但我似乎无法做到这一点。

我有一个简单的测试脚本:

#!/usr/bin/env python

import cgi

cgi.test()

它具有权限 -rwxr-xr-x@ ,位于 ~/WWW (具有权限 drwxr-xr- x)。它在 shell 中运行得很好,我有这个脚本可以使用 CGIHTTPServer 为它们提供服务:

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["~/WWW"]

PORT = 8000

httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT

但是当我运行它时,转到 localhost:8000 只提供脚本的内容,而不是结果(即它返回代码,而不是输出)。

我做错了什么?

I'm trying to set up Python's CGIHTTPServer on Mac OS X to be able to serve CGI scripts locally, but I seem to be unable to do this.

I've got a simple test script:

#!/usr/bin/env python

import cgi

cgi.test()

It has permissions -rwxr-xr-x@ and is located in ~/WWW (with permissions drwxr-xr-x). It runs just fine from the shell and I have this script to serve them using CGIHTTPServer:

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["~/WWW"]

PORT = 8000

httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT

But when I run it, going to localhost:8000 just serves the content of the script, not the result (i.e. it gives back the code, not the output).

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2024-09-20 04:17:34

cgi_directories 中的路径与 URL 的路径部分匹配,而不是实际的文件系统路径。将其设置为 ["/"][""] 可能效果更好。

The paths in cgi_directories are matched against the path part of the URL, not the actual filesystem path. Setting it to ["/"] or [""] will probably work better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文