Python代码可以通过终端正常工作,但不能通过wsgi脚本工作.(TypeError:'NoneType'对象不可迭代)

Mem*_*est -1 python mod-wsgi wsgi python-2.7 python-3.x

完整脚本:

import pprint

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    aaa = ['a','b','c']
    pprint.pprint(aaa)
Run Code Online (Sandbox Code Playgroud)

如果我要在终端上运行它,那就是......

>>> import pprint
>>> aaa = ['a','b','c']
>>> pprint.pprint(aaa)
['a', 'b', 'c']
>>> 
Run Code Online (Sandbox Code Playgroud)

你可以看到它工作正常.但是通过wsgi-script它不起作用.

error_log中:

TypeError:'NoneType'对象不可迭代

BTW是"pprint"PHP中的"print_r()"等价?

Amb*_*ber 9

WSGI要求您要发送回的输出作为函数的返回值返回到浏览器,而不是将其打印出来.所以,你需要使用pprint.pformat()return它的结果,而不是pprint.pprint(这只是尝试通过将其打印出来print-你想在这里不算什么).

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/html')])
    aaa = ['a','b','c']
    return pprint.pformat(aaa)
Run Code Online (Sandbox Code Playgroud)