1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flaskのrequestsモジュールについて、すべてのメソッドを実行してしっかり理解する

Last updated at Posted at 2020-11-25

PythonのFlaskでweb APIを作成することがあるのですが、クエリパラメータの読み込みなどちゃんと理解せずに使ってしまっているので、ここでは、http://127.0.0.1:5000 というURLをFlaskで立てて、そこにPOST通信とGET通信を試みた際の挙動について、requetstsモジュールのすべてのメソッドを実行して調べます。

方法

検証用のflaskの関数とcurlコマンドについて説明します。

Flaskの関数

URLが叩かれたときのrequestモジュールをすべて実行するためのFlaskの関数はこちらです。

main.py
# coding: utf-8
from flask import Flask, request

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def hello():
    request_methods = dir(request)
    print(len(dir(request)),dir(request))
    for i,method in enumerate(request_methods):
        print("%s番目のメソッド:\n  %s"%(i+1,method))
        exec("print('  request.'+method+'の実行結果:',request.%s)"%(method))
        print("")
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Pythonでは、dir()ですべてのメソッドを非再帰的ですが求められますので、これをrequest_methods という変数にリスト形式で保存したあと、文字列をプログラムに変換するexecコマンドを使ってループの中で実行しています。

curlコマンドについて

この関数で立てたURLに対してcurlでGet通信とPost通信を行います。

curl  -H 'Host:some_destination.com.' -H 'Authorization:something' -X GET  "http://127.0.0.1:5000?arg1=myarg1&arg2=myarg2" --insecure

Get通信の場合、基本的には、 http://127.0.0.1:5000?arg1=myarg1&arg2=myarg2 と言う形でクエリパラメータを付与した場合の挙動を調べるようにしています。

curl -X POST -H 'Host:some_destination.com.' -H 'Authorization:something' -F 'image=@./na18_1920x1080_221804.jpg' -F "arg1=myarg1" -F "arg2=myarg2" http://127.0.0.1:5000/ --insecure

Post通信の場合、クエリパラメータを同様に付与することに加えて、HTMLのformから画像をsubmitした場合のrequestモジュールの挙動を調べます。今回は、curlコマンドを打つターミナルの場所にna18_1920x1080_221804.jpgと言う画像ファイルを置いて、それをformからSubmitしています。

以下、Get通信とPost通信をした結果ですが、Requestsモジュールは下記の116個のメソッドも持つため結構長くなります。ご了承ください。

 ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cached_json', '_get_data_for_json', '_get_file_stream', '_get_stream_for_parsing', '_load_form_data', '_parse_content_type', 'accept_charsets', 'accept_encodings', 'accept_languages', 'accept_mimetypes', 'access_control_request_headers', 'access_control_request_method', '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_json', 'is_multiprocess', 'is_multithread', 'is_run_once', 'is_secure', 'json', 'json_module', 'list_storage_class', 'make_form_data_parser', 'max_content_length', 'max_form_memory_size', 'max_forwards', 'method', 'mimetype', 'mimetype_params', 'on_json_loading_failed', 'origin', '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']

結果

Get通信の場合

基本的には43番目のargs、45番目のbase_url66番目のfull_path、68番目のget_json、69番目のheaders,90番目のmethod,98番目のquery_string、109番目のurl,111番目のurl_rootあたりがGet通信では見ておけば良さそうです。

結論のTips

クエリパラメータの取得
main.py
# queryパラメータを取得。ない場合はNoneとなる
arg1 = request.args.get(key='arg1')
header情報の取得
main.py
# ここでは'Authorization'情報を取得。ない場合はNoneとなる
arg1 = request.header.get(key='Authorization')

結果

1番目のメソッド:
  __class__
  request.__class__の実行結果: <class 'werkzeug.local.LocalProxy'>

2番目のメソッド:
  __delattr__
  request.__delattr__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

3番目のメソッド:
  __dict__
  request.__dict__の実行結果: {'environ': {'wsgi.version': (1, 0), 'wsgi.url_scheme': 'http', 'wsgi.input': <_io.BufferedReader name=7>, 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'werkzeug.server.shutdown': <function WSGIRequestHandler.make_environ.<locals>.shutdown_server at 0x10a496840>, 'SERVER_SOFTWARE': 'Werkzeug/1.0.1', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'PATH_INFO': '/', 'QUERY_STRING': 'arg1=myarg1&arg2=myarg2', 'REQUEST_URI': '/?arg1=myarg1&arg2=myarg2', 'RAW_URI': '/?arg1=myarg1&arg2=myarg2', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': 56805, 'SERVER_NAME': '127.0.0.1', 'SERVER_PORT': '5000', 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_HOST': 'some_destination.com.', 'HTTP_USER_AGENT': 'curl/7.64.1', 'HTTP_ACCEPT': '*/*', 'HTTP_AUTHORIZATION': 'something', 'werkzeug.request': <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>}, 'shallow': False, 'url_rule': <Rule '/' (GET, HEAD, POST, OPTIONS) -> hello>, 'view_args': {}, 'url': 'http://some_destination.com./?arg1=myarg1&arg2=myarg2'}

4番目のメソッド:
  __dir__
  request.__dir__の実行結果: <bound method LocalProxy.__dir__ of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

5番目のメソッド:
  __doc__
  request.__doc__の実行結果: Acts as a proxy for a werkzeug local.  Forwards all operations to
    a proxied object.  The only operations not supported for forwarding
    are right handed operands and any kind of assignment.

    Example usage::

        from werkzeug.local import Local
        l = Local()

        # these are proxies
        request = l('request')
        user = l('user')


        from werkzeug.local import LocalStack
        _response_local = LocalStack()

        # this is a proxy
        response = _response_local()

    Whenever something is bound to l.user / l.request the proxy objects
    will forward all operations.  If no object is bound a :exc:`RuntimeError`
    will be raised.

    To create proxies to :class:`Local` or :class:`LocalStack` objects,
    call the object as shown above.  If you want to have a proxy to an
    object looked up by a function, you can (as of Werkzeug 0.6.1) pass
    a function to the :class:`LocalProxy` constructor::

        session = LocalProxy(lambda: get_current_request().session)

    .. versionchanged:: 0.6.1
       The class can be instantiated with a callable as well now.
    

6番目のメソッド:
  __enter__
  request.__enter__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

7番目のメソッド:
  __eq__
  request.__eq__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

8番目のメソッド:
  __exit__
  request.__exit__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

9番目のメソッド:
  __format__
  request.__format__の実行結果: <built-in method __format__ of LocalProxy object at 0x10a5d02d0>

10番目のメソッド:
  __ge__
  request.__ge__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

11番目のメソッド:
  __getattribute__
  request.__getattribute__の実行結果: <method-wrapper '__getattribute__' of LocalProxy object at 0x10a5d02d0>

12番目のメソッド:
  __gt__
  request.__gt__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

13番目のメソッド:
  __hash__
  request.__hash__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

14番目のメソッド:
  __init__
  request.__init__の実行結果: <bound method LocalProxy.__init__ of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

15番目のメソッド:
  __init_subclass__
  request.__init_subclass__の実行結果: <built-in method __init_subclass__ of type object at 0x7feb3d122e18>

16番目のメソッド:
  __le__
  request.__le__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

17番目のメソッド:
  __lt__
  request.__lt__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

18番目のメソッド:
  __module__
  request.__module__の実行結果: werkzeug.local

19番目のメソッド:
  __ne__
  request.__ne__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

20番目のメソッド:
  __new__
  request.__new__の実行結果: <built-in method __new__ of type object at 0x109498d30>

21番目のメソッド:
  __reduce__
  request.__reduce__の実行結果: <built-in method __reduce__ of LocalProxy object at 0x10a5d02d0>

22番目のメソッド:
  __reduce_ex__
  request.__reduce_ex__の実行結果: <built-in method __reduce_ex__ of LocalProxy object at 0x10a5d02d0>

23番目のメソッド:
  __repr__
  request.__repr__の実行結果: <bound method LocalProxy.__repr__ of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

24番目のメソッド:
  __setattr__
  request.__setattr__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

25番目のメソッド:
  __sizeof__
  request.__sizeof__の実行結果: <built-in method __sizeof__ of LocalProxy object at 0x10a5d02d0>

26番目のメソッド:
  __str__
  request.__str__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

27番目のメソッド:
  __subclasshook__
  request.__subclasshook__の実行結果: <built-in method __subclasshook__ of type object at 0x7feb3d122e18>

28番目のメソッド:
  __weakref__
  request.__weakref__の実行結果: None

29番目のメソッド:
  _cached_json
  request._cached_jsonの実行結果: (Ellipsis, Ellipsis)

30番目のメソッド:
  _get_data_for_json
  request._get_data_for_jsonの実行結果: <bound method JSONMixin._get_data_for_json of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

31番目のメソッド:
  _get_file_stream
  request._get_file_streamの実行結果: <bound method BaseRequest._get_file_stream of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

32番目のメソッド:
  _get_stream_for_parsing
  request._get_stream_for_parsingの実行結果: <bound method BaseRequest._get_stream_for_parsing of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

33番目のメソッド:
  _load_form_data
  request._load_form_dataの実行結果: <bound method Request._load_form_data of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

34番目のメソッド:
  _parse_content_type
  request._parse_content_typeの実行結果: <bound method CommonRequestDescriptorsMixin._parse_content_type of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

35番目のメソッド:
  accept_charsets
  request.accept_charsetsの実行結果: 

36番目のメソッド:
  accept_encodings
  request.accept_encodingsの実行結果: 

37番目のメソッド:
  accept_languages
  request.accept_languagesの実行結果: 

38番目のメソッド:
  accept_mimetypes
  request.accept_mimetypesの実行結果: */*

39番目のメソッド:
  access_control_request_headers
  request.access_control_request_headersの実行結果: None

40番目のメソッド:
  access_control_request_method
  request.access_control_request_methodの実行結果: None

41番目のメソッド:
  access_route
  request.access_routeの実行結果: ImmutableList(['127.0.0.1'])

42番目のメソッド:
  application
  request.applicationの実行結果: <bound method BaseRequest.application of <class 'flask.wrappers.Request'>>

43番目のメソッド:
  args
  request.argsの実行結果: ImmutableMultiDict([('arg1', 'myarg1'), ('arg2', 'myarg2')])

44番目のメソッド:
  authorization
  request.authorizationの実行結果: None

45番目のメソッド:
  base_url
  request.base_urlの実行結果: http://some_destination.com./

46番目のメソッド:
  blueprint
  request.blueprintの実行結果: None

47番目のメソッド:
  cache_control
  request.cache_controlの実行結果: 

48番目のメソッド:
  charset
  request.charsetの実行結果: utf-8

49番目のメソッド:
  close
  request.closeの実行結果: <bound method BaseRequest.close of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

50番目のメソッド:
  content_encoding
  request.content_encodingの実行結果: None

51番目のメソッド:
  content_length
  request.content_lengthの実行結果: None

52番目のメソッド:
  content_md5
  request.content_md5の実行結果: None

53番目のメソッド:
  content_type
  request.content_typeの実行結果: None

54番目のメソッド:
  cookies
  request.cookiesの実行結果: ImmutableMultiDict([])

55番目のメソッド:
  data
  request.dataの実行結果: b''

56番目のメソッド:
  date
  request.dateの実行結果: None

57番目のメソッド:
  dict_storage_class
  request.dict_storage_classの実行結果: <class 'werkzeug.datastructures.ImmutableMultiDict'>

58番目のメソッド:
  disable_data_descriptor
  request.disable_data_descriptorの実行結果: False

59番目のメソッド:
  encoding_errors
  request.encoding_errorsの実行結果: replace

60番目のメソッド:
  endpoint
  request.endpointの実行結果: hello

61番目のメソッド:
  environ
  request.environの実行結果: {'wsgi.version': (1, 0), 'wsgi.url_scheme': 'http', 'wsgi.input': <_io.BufferedReader name=7>, 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'werkzeug.server.shutdown': <function WSGIRequestHandler.make_environ.<locals>.shutdown_server at 0x10a496840>, 'SERVER_SOFTWARE': 'Werkzeug/1.0.1', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'PATH_INFO': '/', 'QUERY_STRING': 'arg1=myarg1&arg2=myarg2', 'REQUEST_URI': '/?arg1=myarg1&arg2=myarg2', 'RAW_URI': '/?arg1=myarg1&arg2=myarg2', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': 56805, 'SERVER_NAME': '127.0.0.1', 'SERVER_PORT': '5000', 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_HOST': 'some_destination.com.', 'HTTP_USER_AGENT': 'curl/7.64.1', 'HTTP_ACCEPT': '*/*', 'HTTP_AUTHORIZATION': 'something', 'werkzeug.request': <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>}

62番目のメソッド:
  files
  request.filesの実行結果: ImmutableMultiDict([])

63番目のメソッド:
  form
  request.formの実行結果: ImmutableMultiDict([])

64番目のメソッド:
  form_data_parser_class
  request.form_data_parser_classの実行結果: <class 'werkzeug.formparser.FormDataParser'>

65番目のメソッド:
  from_values
  request.from_valuesの実行結果: <bound method BaseRequest.from_values of <class 'flask.wrappers.Request'>>

66番目のメソッド:
  full_path
  request.full_pathの実行結果: /?arg1=myarg1&arg2=myarg2

67番目のメソッド:
  get_data
  request.get_dataの実行結果: <bound method BaseRequest.get_data of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

68番目のメソッド:
  get_json
  request.get_jsonの実行結果: <bound method JSONMixin.get_json of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

69番目のメソッド:
  headers
  request.headersの実行結果: Host: some_destination.com.
User-Agent: curl/7.64.1
Accept: */*
Authorization: something



70番目のメソッド:
  host
  request.hostの実行結果: some_destination.com.

71番目のメソッド:
  host_url
  request.host_urlの実行結果: http://some_destination.com./

72番目のメソッド:
  if_match
  request.if_matchの実行結果: 

73番目のメソッド:
  if_modified_since
  request.if_modified_sinceの実行結果: None

74番目のメソッド:
  if_none_match
  request.if_none_matchの実行結果: 

75番目のメソッド:
  if_range
  request.if_rangeの実行結果: 

76番目のメソッド:
  if_unmodified_since
  request.if_unmodified_sinceの実行結果: None

77番目のメソッド:
  input_stream
  request.input_streamの実行結果: <_io.BufferedReader name=7>

78番目のメソッド:
  is_json
  request.is_jsonの実行結果: False

79番目のメソッド:
  is_multiprocess
  request.is_multiprocessの実行結果: False

80番目のメソッド:
  is_multithread
  request.is_multithreadの実行結果: True

81番目のメソッド:
  is_run_once
  request.is_run_onceの実行結果: False

82番目のメソッド:
  is_secure
  request.is_secureの実行結果: False

83番目のメソッド:
  json
  request.jsonの実行結果: None

84番目のメソッド:
  json_module
  request.json_moduleの実行結果: <module 'flask.json' from '/Library/Python/3.7/site-packages/flask/json/__init__.py'>

85番目のメソッド:
  list_storage_class
  request.list_storage_classの実行結果: <class 'werkzeug.datastructures.ImmutableList'>

86番目のメソッド:
  make_form_data_parser
  request.make_form_data_parserの実行結果: <bound method BaseRequest.make_form_data_parser of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

87番目のメソッド:
  max_content_length
  request.max_content_lengthの実行結果: None

88番目のメソッド:
  max_form_memory_size
  request.max_form_memory_sizeの実行結果: None

89番目のメソッド:
  max_forwards
  request.max_forwardsの実行結果: None

90番目のメソッド:
  method
  request.methodの実行結果: GET

91番目のメソッド:
  mimetype
  request.mimetypeの実行結果: 

92番目のメソッド:
  mimetype_params
  request.mimetype_paramsの実行結果: {}

93番目のメソッド:
  on_json_loading_failed
  request.on_json_loading_failedの実行結果: <bound method JSONMixin.on_json_loading_failed of <Request 'http://some_destination.com./?arg1=myarg1&arg2=myarg2' [GET]>>

94番目のメソッド:
  origin
  request.originの実行結果: None

95番目のメソッド:
  parameter_storage_class
  request.parameter_storage_classの実行結果: <class 'werkzeug.datastructures.ImmutableMultiDict'>

96番目のメソッド:
  path
  request.pathの実行結果: /

97番目のメソッド:
  pragma
  request.pragmaの実行結果: 

98番目のメソッド:
  query_string
  request.query_stringの実行結果: b'arg1=myarg1&arg2=myarg2'

99番目のメソッド:
  range
  request.rangeの実行結果: None

100番目のメソッド:
  referrer
  request.referrerの実行結果: None

101番目のメソッド:
  remote_addr
  request.remote_addrの実行結果: 127.0.0.1

102番目のメソッド:
  remote_user
  request.remote_userの実行結果: None

103番目のメソッド:
  routing_exception
  request.routing_exceptionの実行結果: None

104番目のメソッド:
  scheme
  request.schemeの実行結果: http

105番目のメソッド:
  script_root
  request.script_rootの実行結果: 

106番目のメソッド:
  shallow
  request.shallowの実行結果: False

107番目のメソッド:
  stream
  request.streamの実行結果: <_io.BytesIO object at 0x10a592c50>

108番目のメソッド:
  trusted_hosts
  request.trusted_hostsの実行結果: None

109番目のメソッド:
  url
  request.urlの実行結果: http://some_destination.com./?arg1=myarg1&arg2=myarg2

110番目のメソッド:
  url_charset
  request.url_charsetの実行結果: utf-8

111番目のメソッド:
  url_root
  request.url_rootの実行結果: http://some_destination.com./

112番目のメソッド:
  url_rule
  request.url_ruleの実行結果: /

113番目のメソッド:
  user_agent
  request.user_agentの実行結果: curl/7.64.1

114番目のメソッド:
  values
  request.valuesの実行結果: CombinedMultiDict([ImmutableMultiDict([('arg1', 'myarg1'), ('arg2', 'myarg2')]), ImmutableMultiDict([])])

115番目のメソッド:
  view_args
  request.view_argsの実行結果: {}

116番目のメソッド:
  want_form_data_parsed
  request.want_form_data_parsedの実行結果: False

Post通信の場合

基本的には43番目のargs、44番目のauthorization、45番目のbase_url,62番目のfiles、63番目のform、69番目のheaders,90番目のmethod,114番目のvaluesあたりがPost通信では見ておけば良さそうです。

1番目のメソッド:
  __class__
  request.__class__の実行結果: <class 'werkzeug.local.LocalProxy'>

2番目のメソッド:
  __delattr__
  request.__delattr__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

3番目のメソッド:
  __dict__
  request.__dict__の実行結果: {'environ': {'wsgi.version': (1, 0), 'wsgi.url_scheme': 'http', 'wsgi.input': <_io.BufferedReader name=5>, 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'werkzeug.server.shutdown': <function WSGIRequestHandler.make_environ.<locals>.shutdown_server at 0x10a496840>, 'SERVER_SOFTWARE': 'Werkzeug/1.0.1', 'REQUEST_METHOD': 'POST', 'SCRIPT_NAME': '', 'PATH_INFO': '/', 'QUERY_STRING': '', 'REQUEST_URI': '/', 'RAW_URI': '/', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': 56815, 'SERVER_NAME': '127.0.0.1', 'SERVER_PORT': '5000', 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_HOST': 'some_destination.com.', 'HTTP_USER_AGENT': 'curl/7.64.1', 'HTTP_ACCEPT': '*/*', 'HTTP_AUTHORIZATION': 'something', 'CONTENT_LENGTH': '1142200', 'CONTENT_TYPE': 'multipart/form-data; boundary=------------------------f915de6b8767f310', 'HTTP_EXPECT': '100-continue', 'werkzeug.request': <Request 'http://some_destination.com./' [POST]>}, 'shallow': False, 'url_rule': <Rule '/' (GET, HEAD, POST, OPTIONS) -> hello>, 'view_args': {}, 'url': 'http://some_destination.com./'}

4番目のメソッド:
  __dir__
  request.__dir__の実行結果: <bound method LocalProxy.__dir__ of <Request 'http://some_destination.com./' [POST]>>

5番目のメソッド:
  __doc__
  request.__doc__の実行結果: Acts as a proxy for a werkzeug local.  Forwards all operations to
    a proxied object.  The only operations not supported for forwarding
    are right handed operands and any kind of assignment.

    Example usage::

        from werkzeug.local import Local
        l = Local()

        # these are proxies
        request = l('request')
        user = l('user')


        from werkzeug.local import LocalStack
        _response_local = LocalStack()

        # this is a proxy
        response = _response_local()

    Whenever something is bound to l.user / l.request the proxy objects
    will forward all operations.  If no object is bound a :exc:`RuntimeError`
    will be raised.

    To create proxies to :class:`Local` or :class:`LocalStack` objects,
    call the object as shown above.  If you want to have a proxy to an
    object looked up by a function, you can (as of Werkzeug 0.6.1) pass
    a function to the :class:`LocalProxy` constructor::

        session = LocalProxy(lambda: get_current_request().session)

    .. versionchanged:: 0.6.1
       The class can be instantiated with a callable as well now.
    

6番目のメソッド:
  __enter__
  request.__enter__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

7番目のメソッド:
  __eq__
  request.__eq__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

8番目のメソッド:
  __exit__
  request.__exit__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

9番目のメソッド:
  __format__
  request.__format__の実行結果: <built-in method __format__ of LocalProxy object at 0x10a5d02d0>

10番目のメソッド:
  __ge__
  request.__ge__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

11番目のメソッド:
  __getattribute__
  request.__getattribute__の実行結果: <method-wrapper '__getattribute__' of LocalProxy object at 0x10a5d02d0>

12番目のメソッド:
  __gt__
  request.__gt__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

13番目のメソッド:
  __hash__
  request.__hash__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

14番目のメソッド:
  __init__
  request.__init__の実行結果: <bound method LocalProxy.__init__ of <Request 'http://some_destination.com./' [POST]>>

15番目のメソッド:
  __init_subclass__
  request.__init_subclass__の実行結果: <built-in method __init_subclass__ of type object at 0x7feb3d122e18>

16番目のメソッド:
  __le__
  request.__le__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

17番目のメソッド:
  __lt__
  request.__lt__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

18番目のメソッド:
  __module__
  request.__module__の実行結果: werkzeug.local

19番目のメソッド:
  __ne__
  request.__ne__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

20番目のメソッド:
  __new__
  request.__new__の実行結果: <built-in method __new__ of type object at 0x109498d30>

21番目のメソッド:
  __reduce__
  request.__reduce__の実行結果: <built-in method __reduce__ of LocalProxy object at 0x10a5d02d0>

22番目のメソッド:
  __reduce_ex__
  request.__reduce_ex__の実行結果: <built-in method __reduce_ex__ of LocalProxy object at 0x10a5d02d0>

23番目のメソッド:
  __repr__
  request.__repr__の実行結果: <bound method LocalProxy.__repr__ of <Request 'http://some_destination.com./' [POST]>>

24番目のメソッド:
  __setattr__
  request.__setattr__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

25番目のメソッド:
  __sizeof__
  request.__sizeof__の実行結果: <built-in method __sizeof__ of LocalProxy object at 0x10a5d02d0>

26番目のメソッド:
  __str__
  request.__str__の実行結果: <bound method LocalProxy.<lambda> of <Request 'http://some_destination.com./' [POST]>>

27番目のメソッド:
  __subclasshook__
  request.__subclasshook__の実行結果: <built-in method __subclasshook__ of type object at 0x7feb3d122e18>

28番目のメソッド:
  __weakref__
  request.__weakref__の実行結果: None

29番目のメソッド:
  _cached_json
  request._cached_jsonの実行結果: (Ellipsis, Ellipsis)

30番目のメソッド:
  _get_data_for_json
  request._get_data_for_jsonの実行結果: <bound method JSONMixin._get_data_for_json of <Request 'http://some_destination.com./' [POST]>>

31番目のメソッド:
  _get_file_stream
  request._get_file_streamの実行結果: <bound method BaseRequest._get_file_stream of <Request 'http://some_destination.com./' [POST]>>

32番目のメソッド:
  _get_stream_for_parsing
  request._get_stream_for_parsingの実行結果: <bound method BaseRequest._get_stream_for_parsing of <Request 'http://some_destination.com./' [POST]>>

33番目のメソッド:
  _load_form_data
  request._load_form_dataの実行結果: <bound method Request._load_form_data of <Request 'http://some_destination.com./' [POST]>>

34番目のメソッド:
  _parse_content_type
  request._parse_content_typeの実行結果: <bound method CommonRequestDescriptorsMixin._parse_content_type of <Request 'http://some_destination.com./' [POST]>>

35番目のメソッド:
  accept_charsets
  request.accept_charsetsの実行結果: 

36番目のメソッド:
  accept_encodings
  request.accept_encodingsの実行結果: 

37番目のメソッド:
  accept_languages
  request.accept_languagesの実行結果: 

38番目のメソッド:
  accept_mimetypes
  request.accept_mimetypesの実行結果: */*

39番目のメソッド:
  access_control_request_headers
  request.access_control_request_headersの実行結果: None

40番目のメソッド:
  access_control_request_method
  request.access_control_request_methodの実行結果: None

41番目のメソッド:
  access_route
  request.access_routeの実行結果: ImmutableList(['127.0.0.1'])

42番目のメソッド:
  application
  request.applicationの実行結果: <bound method BaseRequest.application of <class 'flask.wrappers.Request'>>

43番目のメソッド:
  args
  request.argsの実行結果: ImmutableMultiDict([])

44番目のメソッド:
  authorization
  request.authorizationの実行結果: None

45番目のメソッド:
  base_url
  request.base_urlの実行結果: http://some_destination.com./

46番目のメソッド:
  blueprint
  request.blueprintの実行結果: None

47番目のメソッド:
  cache_control
  request.cache_controlの実行結果: 

48番目のメソッド:
  charset
  request.charsetの実行結果: utf-8

49番目のメソッド:
  close
  request.closeの実行結果: <bound method BaseRequest.close of <Request 'http://some_destination.com./' [POST]>>

50番目のメソッド:
  content_encoding
  request.content_encodingの実行結果: None

51番目のメソッド:
  content_length
  request.content_lengthの実行結果: 1142200

52番目のメソッド:
  content_md5
  request.content_md5の実行結果: None

53番目のメソッド:
  content_type
  request.content_typeの実行結果: multipart/form-data; boundary=------------------------f915de6b8767f310

54番目のメソッド:
  cookies
  request.cookiesの実行結果: ImmutableMultiDict([])

55番目のメソッド:
  data
  request.dataの実行結果: b''

56番目のメソッド:
  date
  request.dateの実行結果: None

57番目のメソッド:
  dict_storage_class
  request.dict_storage_classの実行結果: <class 'werkzeug.datastructures.ImmutableMultiDict'>

58番目のメソッド:
  disable_data_descriptor
  request.disable_data_descriptorの実行結果: False

59番目のメソッド:
  encoding_errors
  request.encoding_errorsの実行結果: replace

60番目のメソッド:
  endpoint
  request.endpointの実行結果: hello

61番目のメソッド:
  environ
  request.environの実行結果: {'wsgi.version': (1, 0), 'wsgi.url_scheme': 'http', 'wsgi.input': <_io.BufferedReader name=5>, 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.multithread': True, 'wsgi.multiprocess': False, 'wsgi.run_once': False, 'werkzeug.server.shutdown': <function WSGIRequestHandler.make_environ.<locals>.shutdown_server at 0x10a496840>, 'SERVER_SOFTWARE': 'Werkzeug/1.0.1', 'REQUEST_METHOD': 'POST', 'SCRIPT_NAME': '', 'PATH_INFO': '/', 'QUERY_STRING': '', 'REQUEST_URI': '/', 'RAW_URI': '/', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': 56815, 'SERVER_NAME': '127.0.0.1', 'SERVER_PORT': '5000', 'SERVER_PROTOCOL': 'HTTP/1.1', 'HTTP_HOST': 'some_destination.com.', 'HTTP_USER_AGENT': 'curl/7.64.1', 'HTTP_ACCEPT': '*/*', 'HTTP_AUTHORIZATION': 'something', 'CONTENT_LENGTH': '1142200', 'CONTENT_TYPE': 'multipart/form-data; boundary=------------------------f915de6b8767f310', 'HTTP_EXPECT': '100-continue', 'werkzeug.request': <Request 'http://some_destination.com./' [POST]>}

62番目のメソッド:
  files
  request.filesの実行結果: ImmutableMultiDict([('image', <FileStorage: 'na18_1920x1080_221804.jpg' ('image/jpeg')>)])

63番目のメソッド:
  form
  request.formの実行結果: ImmutableMultiDict([('arg1', 'myarg1'), ('arg2', 'myarg2')])

64番目のメソッド:
  form_data_parser_class
  request.form_data_parser_classの実行結果: <class 'werkzeug.formparser.FormDataParser'>

65番目のメソッド:
  from_values
  request.from_valuesの実行結果: <bound method BaseRequest.from_values of <class 'flask.wrappers.Request'>>

66番目のメソッド:
  full_path
  request.full_pathの実行結果: /?

67番目のメソッド:
  get_data
  request.get_dataの実行結果: <bound method BaseRequest.get_data of <Request 'http://some_destination.com./' [POST]>>

68番目のメソッド:
  get_json
  request.get_jsonの実行結果: <bound method JSONMixin.get_json of <Request 'http://some_destination.com./' [POST]>>

69番目のメソッド:
  headers
  request.headersの実行結果: Host: some_destination.com.
User-Agent: curl/7.64.1
Accept: */*
Authorization: something
Content-Length: 1142200
Content-Type: multipart/form-data; boundary=------------------------f915de6b8767f310
Expect: 100-continue



70番目のメソッド:
  host
  request.hostの実行結果: some_destination.com.

71番目のメソッド:
  host_url
  request.host_urlの実行結果: http://some_destination.com./

72番目のメソッド:
  if_match
  request.if_matchの実行結果: 

73番目のメソッド:
  if_modified_since
  request.if_modified_sinceの実行結果: None

74番目のメソッド:
  if_none_match
  request.if_none_matchの実行結果: 

75番目のメソッド:
  if_range
  request.if_rangeの実行結果: 

76番目のメソッド:
  if_unmodified_since
  request.if_unmodified_sinceの実行結果: None

77番目のメソッド:
  input_stream
  request.input_streamの実行結果: <_io.BufferedReader name=5>

78番目のメソッド:
  is_json
  request.is_jsonの実行結果: False

79番目のメソッド:
  is_multiprocess
  request.is_multiprocessの実行結果: False

80番目のメソッド:
  is_multithread
  request.is_multithreadの実行結果: True

81番目のメソッド:
  is_run_once
  request.is_run_onceの実行結果: False

82番目のメソッド:
  is_secure
  request.is_secureの実行結果: False

83番目のメソッド:
  json
  request.jsonの実行結果: None

84番目のメソッド:
  json_module
  request.json_moduleの実行結果: <module 'flask.json' from '/Library/Python/3.7/site-packages/flask/json/__init__.py'>

85番目のメソッド:
  list_storage_class
  request.list_storage_classの実行結果: <class 'werkzeug.datastructures.ImmutableList'>

86番目のメソッド:
  make_form_data_parser
  request.make_form_data_parserの実行結果: <bound method BaseRequest.make_form_data_parser of <Request 'http://some_destination.com./' [POST]>>

87番目のメソッド:
  max_content_length
  request.max_content_lengthの実行結果: None

88番目のメソッド:
  max_form_memory_size
  request.max_form_memory_sizeの実行結果: None

89番目のメソッド:
  max_forwards
  request.max_forwardsの実行結果: None

90番目のメソッド:
  method
  request.methodの実行結果: POST

91番目のメソッド:
  mimetype
  request.mimetypeの実行結果: multipart/form-data

92番目のメソッド:
  mimetype_params
  request.mimetype_paramsの実行結果: {'boundary': '------------------------f915de6b8767f310'}

93番目のメソッド:
  on_json_loading_failed
  request.on_json_loading_failedの実行結果: <bound method JSONMixin.on_json_loading_failed of <Request 'http://some_destination.com./' [POST]>>

94番目のメソッド:
  origin
  request.originの実行結果: None

95番目のメソッド:
  parameter_storage_class
  request.parameter_storage_classの実行結果: <class 'werkzeug.datastructures.ImmutableMultiDict'>

96番目のメソッド:
  path
  request.pathの実行結果: /

97番目のメソッド:
  pragma
  request.pragmaの実行結果: 

98番目のメソッド:
  query_string
  request.query_stringの実行結果: b''

99番目のメソッド:
  range
  request.rangeの実行結果: None

100番目のメソッド:
  referrer
  request.referrerの実行結果: None

101番目のメソッド:
  remote_addr
  request.remote_addrの実行結果: 127.0.0.1

102番目のメソッド:
  remote_user
  request.remote_userの実行結果: None

103番目のメソッド:
  routing_exception
  request.routing_exceptionの実行結果: None

104番目のメソッド:
  scheme
  request.schemeの実行結果: http

105番目のメソッド:
  script_root
  request.script_rootの実行結果: 

106番目のメソッド:
  shallow
  request.shallowの実行結果: False

107番目のメソッド:
  stream
  request.streamの実行結果: <werkzeug.wsgi.LimitedStream object at 0x10a593e48>

108番目のメソッド:
  trusted_hosts
  request.trusted_hostsの実行結果: None

109番目のメソッド:
  url
  request.urlの実行結果: http://some_destination.com./

110番目のメソッド:
  url_charset
  request.url_charsetの実行結果: utf-8

111番目のメソッド:
  url_root
  request.url_rootの実行結果: http://some_destination.com./

112番目のメソッド:
  url_rule
  request.url_ruleの実行結果: /

113番目のメソッド:
  user_agent
  request.user_agentの実行結果: curl/7.64.1

114番目のメソッド:
  values
  request.valuesの実行結果: CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('arg1', 'myarg1'), ('arg2', 'myarg2')])])

115番目のメソッド:
  view_args
  request.view_argsの実行結果: {}

116番目のメソッド:
  want_form_data_parsed
  request.want_form_data_parsedの実行結果: True

これで次以降ちゃんとできるはず・・・!!

この記事が役に立ったと思ったらLGTMお願いいたします:thumbsup:

最後に自分用のメモ。
GCFで検証する場合、pythonファイルは下記。

main.py
# coding: utf-8
from flask import Flask, request

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def hello(request):
    request_methods = dir(request)
    print(len(dir(request)),dir(request))
    for i,method in enumerate(request_methods):
        print("%s番目のメソッド:\n  %s"%(i+1,method))
        exec("print('  request.'+method+'の実行結果:',request.%s)"%(method))
        print("")
    return "Hello World!"

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?