2017-06-28 14 views
0

Python 3.4 mod_wsgiアプリケーションサーバーが例外をスローしたときにブラウザにトレースバックを表示するために、Python Paste 2.0.3に含まれるErrorMiddlewareを使用しようとしています。Python 3 mod_wsgiアプリケーションでErrorMiddlewareを貼り付けると例外がスローされるのはなぜですか?

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/usr/lib/python3.4/site-packages/paste/exceptions/errormiddleware.py", line 154, in __call__ 
    response = self.exception_handler(exc_info, environ) 
    File "/usr/lib/python3.4/site-packages/paste/exceptions/errormiddleware.py", line 188, in exception_handler 
    simple_html_error=simple_html_error) 
    File "/usr/lib/python3.4/site-packages/paste/exceptions/errormiddleware.py", line 391, in handle_exception 
    error_stream.write(line) 
TypeError: must be str, not bytes 

ので、私はまだちょうどブラウザで500 Internal Server Errorを取得しています:私が午前問題は、それが私のサーバーの例外を処理しようとするたびErrorMiddlewareは、この例外をスローしていることです。

問題がErrorMiddlewareはWSGIエラーストリームにバイトを書き込みしようとしていることのようだ:

if six.PY3: 
     line = line.encode('utf8') 
    error_stream.write(line) # line 391 error_stream comes from environ['wsgi.errors'] 

が、私はenviron['wsgi.errors']をプリントアウトした場合、それはテキストストリームのように見える:

'wsgi.errors': <_io.TextIOWrapper name='<wsgi.errors>' encoding='utf-8'> 

これは、ErrorMiddlewareがバイトを書き込むことができないことを意味します。

私はこのようErrorMiddlewareで自分のアプリケーションをラップしています:

application = ErrorMiddleware(application, debug=True) 

はこれを引き起こす私が間違っているの何かがありますか?エラーストリームがテキストストリームではなくバイトストリームになるようにmod_wsgiを設定できますか?

答えて

1

wsgi.errorsは、概念的には、sys.stdoutに書き込むと同じように動作する必要があります。

$ python3.6 
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sys 
>>> sys.stdout.write('hello\n') 
hello 
6 
>>> sys.stdout.write(b'hello\n') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: write() argument must be str, not bytes 

だから、彼らは私がそうだった考え出し何の一種だPythonの3

+0

でバイトを書き込もうとすることを間違って見えます。確認していただきありがとうございます。 – gla3dr

関連する問題