3

所以我在我的服务器上安装了 python,并且我将 wsgi_module 与 apache 一起使用。

我所有的python程序都必须使用这种格式吗:

def application(environ, start_response):
    headers = []
    headers.append(('Content-Type', 'text/plain'))
    write = start_response('200 OK', headers)

    input = environ['wsgi.input']
    output = cStringIO.StringIO()

    print >> output, "test"

    output.write(input.read(int(environ.get('CONTENT_LENGTH', '0'))))
    return [output.getvalue()]

无论如何要设置它,这样我就可以编写python脚本并且只需:

print "test"
4

1 回答 1

2

是的,您的脚本当然需要具有可调用的(不一定是函数,它也可以是定义了 __call__ 方法的类),它接受environ和 *start_response* 参数,因为它是wsgi 标准的一部分,这就是它的工作方式.

您想以 PHP 风格编写脚本(例如“打印某些东西”)的方式行不通。目前有一个名为PSP (Python Server Pages) 的项目,它混合了代码和模板,但它并没有流行起来,因为它肯定不是 Python 编写应用程序的方式。

于 2012-08-10T10:55:52.040 回答