所以我在我的服务器上安装了 python,并且我正在使用带有 apache 的 wsgi_module。
我所有的 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"
请您参考如下方法:
是的,你的脚本当然需要有callable(不一定是函数,它也可以是定义了__call__方法的类)接受environ和*start_response*参数,因为它是 wsgi standard 的一部分这就是它的工作方式。
您希望以 PHP 风格编写脚本(如“打印某些内容”)的方式行不通。有一个目前已死的倡议最初称为 PSP (Python 服务器页面)混合了代码和模板,但它无法流行,因为它肯定不是编写应用程序的 Python 方式。
