【问题标题】:Python code works okay via terminal, but not via wsgi script. ( TypeError: 'NoneType' object is not iterable )Python 代码可以通过终端运行,但不能通过 wsgi 脚本运行。 (TypeError:'NoneType' 对象不可迭代)
【发布时间】:2013-08-04 19:33:56
【问题描述】:

完整脚本:

import pprint

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

如果我要在终端中运行它会是......

>>> import pprint
>>> aaa = ['a','b','c']
>>> pprint.pprint(aaa)
['a', 'b', 'c']
>>> 

如您所见,它运行良好。但是通过 wsgi-script 它不起作用。

错误日志:

TypeError: 'NoneType' 对象不可迭代

顺便说一句,“pprint”是 PHP 中的“print_r()”等价物?

【问题讨论】:

  • :-) 苏美尔,你并不像你认为的那样聪明或隐秘。但我确实为风格编辑了很多问题,而这个问题还没有消失。
  • 赞成,合理的问题; “将响应作为可迭代返回”模式对于任何习惯使用 CGI、php 的人来说都不是很直观

标签: python python-2.7 python-3.x mod-wsgi wsgi


【解决方案1】:

WSGI 要求您return 将要发送回浏览器的输出作为函数的返回值,而不仅仅是打印出来。所以你需要使用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)

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多