Skip to content

Instantly share code, notes, and snippets.

@akash0x53
Last active August 29, 2015 14:11
Show Gist options
  • Save akash0x53/49b80559bdf1384c4566 to your computer and use it in GitHub Desktop.
Save akash0x53/49b80559bdf1384c4566 to your computer and use it in GitHub Desktop.
Python decorator with argument to check HTTP request method.
class CheckHttpMethod(object):
"""
Decorator check whether HTTP request is as per your need,
if not it redirects to '/'.
This is a decorator with *mandatory* argument, a HTTP method
you want to check.
>>>@CheckHttpMethod('POST')
...def handler(*args, *kwargs):
... pass
"""
def __init__(self, method=None):
assert method, None
self.method = method
def __call__(self, fn):
def __check_method(method, *args, **kwargs):
if method != self.method:
return web.seeother('/')
return fn(method, *args, **kwargs)
return __check_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment