This informational PEP clarifies how bytes/text issues are to be handled by the WGSI protocol. The challenge is that string handling in Python 3 is most conveniently handled with the str
type even though the HTTP protocol is itself bytes oriented.
この情報提供のための PEP は、WSGI プロトコルにおいてバイト/テキストの問題がどう扱われるべきかを明確にする。 課題は、HTTP プロトコル自体がバイト列指向であるのに対し、Python 3 における文字列処理は str
型を使うのがいちばん便利になっていることである。
The PEP differentiates so-called native strings that are used for request/response headers and metadata versus byte strings which are used for the bodies of requests and responses.
この PEP では、いわゆるネイティブ文字列という言葉をリクエスト/レスポンスのヘッダおよびメタデータに対して使い、バイト文字列という言葉をリクエストとレスポンスのボディに対して使う。
The native strings are always of type str
but are restricted to code points between U+0000 through U+00FF which are translatable to bytes using Latin-1 encoding. These strings are used for the keys and values in the environment dictionary and for response headers and statuses in the start_response
function. They must follow 2616
with respect to encoding. That is, they must either be ISO-8859-1 characters or use 2047
MIME encoding.
ネイティブ文字列は常に str
型を使うが、その値はコードポイントで U+0000 から U+00FF までの、Latin-1 エンコーディングで bytes に変換できる範囲に制限される。 これらの文字列は、環境 (environment) を表す辞書のキーと値で、また関数 start_response
でのレスポンスヘッダとステータスで使われる。 それらはエンコーディングに関して 2616
を満たさなければならない。 つまり、ISO-8859-1 文字であるか、または 2047
MIME エンコーディングを使わなければならない。
For developers porting WSGI applications from Python 2, here are the salient points:
WSGI アプリケーションを Python 2 から移植する開発者は、次の主だった点に注意すること:
If the app already used strings for headers in Python 2, no change is needed.
アプリケーションがすでに Python 2 でヘッダ部分に文字列を使っている場合、何も変更する必要はない。
If instead, the app encoded output headers or decoded input headers, then the headers will need to be re-encoded to Latin-1. For example, an output header encoded in utf-8 was using
h.encode('utf-8')
now needs to convert from bytes to native strings usingh.encode('utf-8').decode('latin-1')
.そうでないなら、つまりアプリケーションが出力用ヘッダをエンコードしたり入力用ヘッダをデコードしている場合は、それらのヘッダは Latin-1 へ再度エンコードされなければならない。 たとえば、
h.encode('utf-8')
を用いて utf-8 でエンコードされていた出力用ヘッダは、h.encode('utf-8').decode('latin-1')
を使ってバイト列からネイティブ文字列へ変換する必要がある。Values yielded by an application or sent using the
write
method must be byte strings. Thestart_response
function and environ must use native strings. The two cannot be mixed.アプリケーションによって生成されるデータや、あるいは
write
メソッドを使って送信されるデータは、バイト文字列でなければならない。 関数start_response
と環境 (environ) はネイティブ文字列を使わなければならない。 両者を混ぜることはできない。
For server implementers writing CGI-to-WSGI pathways or other CGI-style protocols, the users must to be able access the environment using native strings even though the underlying platform may have a different convention. To bridge this gap, the wsgiref
module has a new function, wsgiref.handlers.read_environ
for transcoding CGI variables from os.environ
into native strings and returning a new dictionary.
CGI から WSGI への橋渡し (pathway) または CGI に似た他のプロトコロルを書いているようなサーバの実装者は、たとえ下位のプラットフォームが異なる条約を使っていたとしても、ユーザがネイティブ文字列を使って環境にアクセスできるようにしなければならない。 このギャップを埋めるために、wsgiref
モジュールに新しい関数 wsgiref.handlers.read_environ
が追加された。 この関数は、os.environ
から CGI 変数を取得してネイティブ文字列へ変換し、新しい辞書として返す。
3333
- Python Web Server Gateway Interface v1.0.1PEP written by Phillip Eby.