使用 WSGI 在 200 之前返回 HTTP 1xx 代码
Returning a HTTP 1xx code before the 200 one with WSGI
我写了一个 Python+WSGI 应用程序,我想 return 一个临时的 1xx 状态代码,例如 102 "Processing" 或 103 "Early hints" 和一些 headers,在 return 最后的 200 和结果 body.
之前
我知道,要通过几个步骤 return 获取数据,我的应用程序需要可迭代,例如使用 yield(参见 In WSGI, send response without returning )
但是到目前为止我发现的所有示例都只使用一个状态代码。我找不到改变它的方法。比如代码:
import wsgiref, wsgiref.simple_server, time
def app(environ, start):
start('102 Processing', [('Foo', 'bar')])
yield "More to come"
time.sleep(2)
start('200 OK', [('Content-Type', 'text/plain')])
yield "hello, world"
httpd = wsgiref.simple_server.make_server('localhost', 8999, app)
httpd.serve_forever()
只发送102状态码,发送完就崩溃:
Exception happened during processing of request from ('127.0.0.1', 53540)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
self.handle()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 131, in handle
handler.run(self.server.get_app())
File "/usr/lib/python2.7/wsgiref/handlers.py", line 92, in run
self.close()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
WSGI 中有没有办法做到这一点?
没有。在 WSGI 应用程序级别,这是不可能的。
状态和headers只能更改到第一个响应内容产生之前,但此时还不允许将任何内容发送回客户端,所以它不能' t用于发送多个状态和headers,只替换将要发送的内容。
我写了一个 Python+WSGI 应用程序,我想 return 一个临时的 1xx 状态代码,例如 102 "Processing" 或 103 "Early hints" 和一些 headers,在 return 最后的 200 和结果 body.
之前我知道,要通过几个步骤 return 获取数据,我的应用程序需要可迭代,例如使用 yield(参见 In WSGI, send response without returning )
但是到目前为止我发现的所有示例都只使用一个状态代码。我找不到改变它的方法。比如代码:
import wsgiref, wsgiref.simple_server, time
def app(environ, start):
start('102 Processing', [('Foo', 'bar')])
yield "More to come"
time.sleep(2)
start('200 OK', [('Content-Type', 'text/plain')])
yield "hello, world"
httpd = wsgiref.simple_server.make_server('localhost', 8999, app)
httpd.serve_forever()
只发送102状态码,发送完就崩溃:
Exception happened during processing of request from ('127.0.0.1', 53540)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
self.handle()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 131, in handle
handler.run(self.server.get_app())
File "/usr/lib/python2.7/wsgiref/handlers.py", line 92, in run
self.close()
File "/usr/lib/python2.7/wsgiref/simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
WSGI 中有没有办法做到这一点?
没有。在 WSGI 应用程序级别,这是不可能的。
状态和headers只能更改到第一个响应内容产生之前,但此时还不允许将任何内容发送回客户端,所以它不能' t用于发送多个状态和headers,只替换将要发送的内容。