私はPython + WSGIアプリケーションを書いていますが、戻す前にいくつかのヘッダーで102 "Processing"や103 "Early hints"などの暫定1xxステータスコードを返したいと思います。最後の200と結果のボディ。私が知っているWSGIで200 1の前にHTTP 1xxコードを返す
、それは、いくつかの手順でデータを返すために、私のアプリケーションは、収率で、たとえば
を(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でありさえ方法は何ですか?
私が見たRFCによれば、これはおそらく可能ですが、あなたが持っている問題はヘッダーの後に応答(テキスト)を送り、別のヘッダーを送信しようとしていると思います。あなたのタイマーの前にその最初の '収量'を取り除き、それがエラーを止めるかどうかを見てください。暫定ヘッダはコード100-199のみを送信することが許可されているので、2xx、3xx、4xx、または5xx応答ヘッダの前に1xxコードを送信する限り、OKでなければなりません。少なくともそれは私に読まれる方法です。 – DDeMartini
@DDeMartini申し訳ありませんが、最初の歩留まりを削除すると何も変わりませんでした。 – bortzmeyer
エラーは、「start_responseが既に呼び出されている場合は、exc_info引数を指定しないでstart_responseを呼び出すと致命的なエラーになります」(https://www.python.org/dev/peps/pep-3333/#the-start-response - 呼び出し可能)。私はWSGIがあなたが望むものをサポートしているかどうかはわかりません。 – kmkaplan