<Flask, Python> リクエストコンテキストで遊ぶ。

リクエストコンテキストrequest contextで遊ぶ。
つまり、リクエストを受けた状態、で遊ぶ。

その1

withrequest context状態にする。

In [1]: from flask import request, Flask

In [2]: app = Flask('aaa')

In [3]: with app.test_request_context('/hello', method='POST'):
   ...:     print(request.path)
   ...:     
/hello

その2

RequestContextをプッシュpushする。
こっちの方が遊びやすい。

In [9]: ctx = app.test_request_context('/hello', method='POST')

In [10]: ctx.push()

In [11]: request.url
Out[11]: 'http://localhost/hello'

In [12]: type(ctx)
Out[12]: flask.ctx.RequestContext

In [13]: dir(ctx)
Out[13]: 
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_after_request_functions',
 '_implicit_app_ctx_stack',
 '_preserved_exc',
 'app',
 'auto_pop',
 'copy',
 'flashes',
 'g',
 'match_request',
 'pop',
 'preserved',
 'push',
 'request',
 'session',
 'url_adapter']

In [14]: type(request)
Out[14]: werkzeug.local.LocalProxy

In [15]: dir(request)
Out[15]: 
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_get_file_stream',
 '_get_stream_for_parsing',
 '_is_old_module',
 '_load_form_data',
 '_parse_content_type',
 'accept_charsets',
 'accept_encodings',
 'accept_languages',
 'accept_mimetypes',
 'access_route',
 'application',
 'args',
 'authorization',
 'base_url',
 'blueprint',
 'cache_control',
 'charset',
 'close',
 'content_encoding',
 'content_length',
 'content_md5',
 'content_type',
 'cookies',
 'data',
 'date',
 'dict_storage_class',
 'disable_data_descriptor',
 'encoding_errors',
 'endpoint',
 'environ',
 'files',
 'form',
 'form_data_parser_class',
 'from_values',
 'full_path',
 'get_data',
 'get_json',
 'headers',
 'host',
 'host_url',
 'if_match',
 'if_modified_since',
 'if_none_match',
 'if_range',
 'if_unmodified_since',
 'input_stream',
 'is_multiprocess',
 'is_multithread',
 'is_run_once',
 'is_secure',
 'is_xhr',
 'json',
 'list_storage_class',
 'make_form_data_parser',
 'max_content_length',
 'max_form_memory_size',
 'max_forwards',
 'method',
 'mimetype',
 'mimetype_params',
 'module',
 'on_json_loading_failed',
 'parameter_storage_class',
 'path',
 'pragma',
 'query_string',
 'range',
 'referrer',
 'remote_addr',
 'remote_user',
 'routing_exception',
 'scheme',
 'script_root',
 'shallow',
 'stream',
 'trusted_hosts',
 'url',
 'url_charset',
 'url_root',
 'url_rule',
 'user_agent',
 'values',
 'view_args',
 'want_form_data_parsed']

マニュアル。

http://flask.pocoo.org/docs/0.12/quickstart/#context-locals

http://flask.pocoo.org/docs/0.12/shell/#creating-a-request-context