在 WSGI 中,发送响应而不返回 [英] In WSGI, send response without returning

查看:36
本文介绍了在 WSGI 中,发送响应而不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以包装一个 WSGI 应用程序方法,这样服务器会在调用特定方法时发送响应,而不是在应用程序方法返回序列时发送响应?

Is there any way to wrap a WSGI application method such the server will send a response when a particular method is called, rather than when the application method returns a sequence?

基本上,我想要一个与 finish_response(responseValue) 方法等效的方法.

Basically, I'd like to have the equivalent of a finish_response(responseValue) method.

推荐答案

WSGI 应用程序必须以某种方式返回一个可迭代对象.如果您想发送部分响应,请将您的应用程序变成一个生成器(或返回一个迭代器):

WSGI app must return an iterable, one way or another. If you want to send partial responses, turn your application into a generator (or return an iterator):

import wsgiref, wsgiref.simple_server, time

def app(environ, start):
    start('200 OK', [('Content-Type', 'text/plain')])

    yield 'hello '
    time.sleep(10)
    yield 'world'

httpd = wsgiref.simple_server.make_server('localhost', 8999, app)
httpd.serve_forever()

这篇关于在 WSGI 中,发送响应而不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆