【问题标题】:Allow ALL method types in flask route允许烧瓶路线中的所有方法类型
【发布时间】:2013-05-17 14:49:56
【问题描述】:

如何让路由接受所有类型的方法?

我不只是想路由HEADGETPOSTOPTIONSDELETEPUT 等标准方法。

我希望它也接受以下方法:FOOBARWHYISTHISMETHODNAMESOLONG & 所有其他可能的方法名称。

【问题讨论】:

    标签: python flask


    【解决方案1】:

    要快速为route 启用所有HTTP Request Methods 而无需手动将规则添加到Flask url_map,修改route 定义如下:

    from flask import request
    
    HTTP_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH']
    
    
    @app.route('/', methods=HTTP_METHODS)
    def index():
      return request.method
    

    【讨论】:

      【解决方案2】:

      您可以为此直接更改 url_map,方法是添加 Rule 而不使用任何方法:

      from flask import Flask, request
      import unittest
      from werkzeug.routing import Rule
      
      app = Flask(__name__)
      app.url_map.add(Rule('/', endpoint='index'))
      
      @app.endpoint('index')
      def index():
          return request.method
      
      
      class TestMethod(unittest.TestCase):
      
          def setUp(self):
              self.client = app.test_client()
      
          def test_custom_method(self):
              resp = self.client.open('/', method='BACON')
              self.assertEqual('BACON', resp.data)
      
      if __name__ == '__main__':
          unittest.main()
      

      methods

      此规则适用的一系列 http 方法。如果未指定,则允许所有方法。

      【讨论】:

        【解决方案3】:

        见下文,这是一些代码(我已删减)from the Flask app object。此代码处理添加 url 规则(当您在视图上执行 app.route() 时,flask 也会调用该规则)....

        @setupmethod
        def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
            """ I remove a ton the documentation here.... """
        
            if endpoint is None:
                endpoint = _endpoint_from_view_func(view_func)
            options['endpoint'] = endpoint
            methods = options.pop('methods', None)
        
            # if the methods are not given and the view_func object knows its
            # methods we can use that instead.  If neither exists, we go with
            # a tuple of only `GET` as default.
            if methods is None:
                methods = getattr(view_func, 'methods', None) or ('GET',)
            methods = set(methods)
        
            # ... SNIP a bunch more code...
            rule = self.url_rule_class(rule, methods=methods, **options)
            rule.provide_automatic_options = provide_automatic_options
        
            self.url_map.add(rule)
        

        如您所见,Flask 将尽最大努力确保明确定义方法。现在,Flask 是基于 Werkzeug 的,而且线...

        rule = self.url_rule_class(rule, methods=methods, **options)
        

        ...通常使用Werkzeug's Rule 类。此类具有以下有关“方法”参数的文档...

        此规则适用的一系列 http 方法。如果没有指定,所有 方法是允许的。

        所以,这告诉我,您可能能够执行以下操作...

        from werkzeug.routing import Rule
        
        app = Flask(__name__)
        
        def my_rule_wrapper(rule, **kwargs):
            kwargs['methods'] = None
            return Rule(rule, **kwargs)
        
        app.url_rule_class = my_rule_wrapper
        

        我尚未对此进行测试,但希望这可以让您走上正轨。

        编辑:

        或者您可以使用 DazWorrall 的答案,这似乎更好:P

        【讨论】:

        • 更改 url_rule_class 也会起作用 :) 如果您想打开许多(或全部)路由到任何方法,它可能更干净。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-09
        • 2014-07-28
        • 1970-01-01
        • 2014-03-08
        • 1970-01-01
        • 2020-07-30
        相关资源
        最近更新 更多