【问题标题】:Python WSGI Error on Output输出上的 Python WSGI 错误
【发布时间】:2015-04-30 04:50:55
【问题描述】:

我的 python 已经生锈了,因为我已经有一段时间没有使用它了,并且遇到了一个我无法解决的问题。

python 程序应该接受网页发送的表单数据,并返回一些 JSON 响应代码给它。除非发送数据,否则一切正常:

def application(environ, start_response):
    """ Process the form data, spit out the reponse """

    # code here extracts the form 
    # data and the response code is 
    # stored in info

    # everything done, output now
    output(environ, start_response, info)


def output(environ, start_response, info):
    
    # debug
    print >> environ['wsgi.errors'], "BACK TO THE BROWSER"

    feedback = json.dumps(info)
    
    # debug
    print >> environ['wsgi.errors'], feedback
    
    status = "200 OK"
    headers = [('Content-Type', 'application/json; charset=UTF-8'), ('Content-Length', str(len(feedback)))]
    start_response(status, headers)
    return [feedback]

错误日志说:

...
[wsgi:error] ...返回浏览器

[wsgi:error] ... {“错误”:[1430360477881, 1430233459050]}

[wsgi:error] ... mod_wsgi (pid=1654): 处理 WSGI 脚本 '/tmp/mod_wsgi-localhost:8000:0/htdocs/' 时发生异常。

[wsgi:error] ... TypeError: 'NoneType' 对象不可迭代
...

好的,Python 抱怨它期望可迭代的东西是 None。但是 Python 在这个函数中抱怨的是哪个迭代? (请注意,我直接使用 mod_wsgi,没有任何框架/模块)。

【问题讨论】:

    标签: python mod-wsgi wsgi


    【解决方案1】:

    您需要返回响应:

    return output(environ, start_response, info)
    

    【讨论】:

    • 所以基本上,对于 WSGI,输入和输出都必须来自同一个函数(这里是 application(),在 mod_wsgi 的情况下)?
    • WSGI 需要application 来接受请求并返回一个可迭代的响应。
    • 更多详情请见PEP 3333
    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多