request

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

request

requestオブジェクトはPythonのdictクラスを拡張したgluon.storage.Storageという普遍的なweb2pyのクラスのインスタンスです。基本的には辞書ですが、アイテムの値は属性としてアクセスすることができます:

The request object is an instance of the ubiquitous web2py class that is calledgluon.storage.Storage, which extends the Python dict class. It is basically a dictionary, but the item values can also be accessed as attributes:

1.

request.vars

これは次と同じです:

is the same as:

1.

request['vars']

辞書とは異なり、属性(またはキー)がない場合、例外を発生させず、代わりに、Noneを返します。requestは以下のアイテム/属性を持ちます。そのうちのいくつかはまたStorageクラスのインスタンスです:

Unlike a dictionary, if an attribute (or key) does not exist, it does not raise an exception. Instead, it returns None. request has the following items/attributes, some of which are also an instance of the Storage class:

  • request.cookies: HTTPリクエストにより渡されたクッキーを格納するCookie.SimpleCookie()オブジェクトです。クッキーの辞書のように動作します。各クッキーはMorselオブジェクトです。

  • a Cookie.SimpleCookie() object containing the cookies passed with the HTTP request. It acts like a dictionary of cookies. Each cookie is a Morsel object.

  • request.env: コントローラに渡される環境変数を格納するStorageオブジェクトです。HTTPリクエストからのHTTPヘッダ変数と標準のWSGIパラメータを含みます。環境変数はすべて小文字に変換され、覚えやすいようにドットはアンダースコアに変換されます。

  • a Storage object containing the environment variables passed to the controller, including HTTP header variables from the HTTP request and standard WSGI parameters. The environment variables are all converted to lower case, and dots are converted to underscores for easier memorization.

  • request.application: リクエストされたアプリケーションの名前です(request.env.path_infoからパースされる)。

    • the name of the requested application (parsed fromrequest.env.path_info).

  • request.controller: リクエストされたコントローラの名前です(request.env.path_infoからパースされる)。

    • the name of the requested controller (parsed from therequest.env.path_info).

  • request.function: リクエストされた関数の名前です(request.env.path_infoからパースされる)。

    • the name of the requested function (parsed from therequest.env.path_info).

  • request.extension: リクエストアクションの拡張子です。デフォルトは”html”です。もしコントローラの関数が辞書を返し、かつビューが指定されなかった場合、辞書をレンダリングするビューファイルの拡張子を決めるために利用されます(request.env.path_infoからパースされる)。

  • the extension of the requested action. It defaults to "html". If the controller function returns a dictionary and does not specify a view, this is used to determine the extension of the view file that will render the dictionary (parsed from therequest.env.path_info).

  • request.folder: アプリケーションのディレクトリです。たとえばアプリケーションが"welcome"の場合、request.folderには"/path/to/welcome"という絶対パスが設定されます。プログラムにおいて、アクセスするファイルへのパスを構築するには、この変数とos.path.join関数を常に使用すべきです。web2pyはたいてい絶対パスを使用していますが、現在の作業フォルダは(何であれ)決して変更しないということが、良いルールです。スレッドセーフなプラクティスではないからです。

  • the application directory. For example if the application is "welcome",request.folder is set to the absolute path "/path/to/welcome". In your programs, you should always use this variable and the os.path.join function to build paths to the files you need to access. Although web2py always uses absolute paths, it is a good rule never to explicitly change the current working folder (whatever that is) since this is not a thread-safe practice.

  • request.now: 現在のリクエストのタイムスタンプを保存したdatetime.datetimeオブジェクトです。

    • a datetime.datetime object storing the time-stamp of the current request.

  • request.args: コントローラの関数名の後に続く、URLパス成分のリスト。request.env.path_info.split('/')[3:]と等しいです。

    • A list of the URL path components following the controller function name; equivalent to request.env.path_info.split('/')[3:]

  • request.vars: gluon.storage.HTTP GETとHTTP POSTのクエリ変数を格納するgluon.storage.Storageオブジェクトです。

    • a gluon.storage.Storage object containing the HTTP GET and HTTP POST query variables.

  • request.get_vars: gluon.storage.HTTP GETのクエリ変数のみを格納するgluon.storage.Storageオブジェクトです。

    • a gluon.storage.Storage object containing only the HTTP GET query variables.

  • request.post_vars: HTTP POSTのクエリ変数のみを格納するgluon.storage.Storageオブジェクトです。

    • a gluon.storage.Storage object containing only the HTTP POST query variables.

  • request.client: クライアントのIPアドレスです。提示されていれば、request.env.http_x_forwarded_forから、そうでない場合は、request.env.remote_addrから決定されます。これは有用ですが、http_x_forwarded_forは偽装することができるため信頼すべきものではありません。

  • The ip address of the client as determined by, if present,request.env.http_x_forwarded_for or by request.env.remote_addr otherwise. While this is useful it should not be trusted because the http_x_forwarded_for can be spoofed.

  • request.body: HTTPリクエストのボディが含まれている、読み取り専用ファイルストリームです。これはrequest.post_varsを取得するために自動的に解析されます。解析後は、ストリームの位置は戻ります。request.body.read()で読むことができます。

  • a read-only file stream that contains the body of the HTTP request. This is automatically parsed to get the request.post_vars and then rewinded. It can be read with request.body.read().

  • request.ajax は、呼ばれる関数がAjaxリクエストを介したものだとTrueになります。

    • is True if the function is being called via an Ajax request.

  • request.cid は、(任意の)Ajaxリクエストを生成したコンポーネントのidです。コンポーネントの詳細については第13章を読んでください。

  • is the id of the component that generated the Ajax request (if any). You can read more about components in Chapter 13.

  • request.wsgi は、アプリケーション内部からサードパーティのWSGIアプリケーションを呼び出せるようにするフックです。

    • a hook that allows to call third party WSGI applications from inside actions.

例として、典型的なシステム上での次の呼び出しは:

As an example, the following call on a typical system:

http://127.0.0.1:8000/examples/default/status/x/y/z?p=1&q=2

次の表のようになります:

results in the following table:

どのような環境変数が実際に定義されているかは、Webサーバーによって異なります。この例では、組み込みまれたRocket wsgiのサーバーを想定しています。変数のセットはApacheのWebサーバーを使用した場合でも大きな違いはありません。

Which environment variables are actually defined depends on the web server. Here we are assuming the built-in Rocket wsgi server. The set of variables is not much different when using the Apache web server.

request.env.http_*変数は、HTTPリクエストヘッダから解析されたものです。

The request.env.http_* variables are parsed from the request HTTP header.

request.env.web2py_*変数。これらは、Webサーバー環境から解析されたものではありません。web2pyの実行場所やバージョン、あるいはGoogle App Engine上で動いているかどうか(個別の最適化が必要かもしれないため)ということをアプリケーションが知るために、web2pyによって作成されます。

The request.env.web2py_* variables. These are not parsed from the web server environment, but are created by web2py in case your applications need to know about the web2py location and version, and whether it is running on the Google App Engine (because specific optimizations may be necessary).

また、request.env.wsgi_*変数もあります。これらはWSGIアダプタに固有です。

Also notice the request.env.wsgi_* variables. They are specific to the wsgi adaptor.