本文整理汇总了Python中webapp2.RequestHandler方法的典型用法代码示例。如果您正苦于以下问题:Python webapp2.RequestHandler方法的具体用法?Python webapp2.RequestHandler怎么用?Python webapp2.RequestHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类webapp2
的用法示例。
在下文中一共展示了webapp2.RequestHandler方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MakeCommandHandler
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def MakeCommandHandler(cmd_cls):
"""Takes a command class and returns a route tuple which allows that command
to be executed.
"""
class H(webapp2.RequestHandler):
def get(self):
self.response.write("""
<h1>You are about to run command "{}". Are you sure?</h1>
<form action="" method="POST">
<button>Punch it</button>
</form>""".format(self._get_cmd().NAME))
def post(self):
deferred.defer(self._get_cmd().run)
self.response.write('Command started.')
def _get_cmd(self):
if 'cmds' not in self.app.registry:
self.app.registry['cmds'] = {}
if cmd_cls.SHORT_NAME not in self.app.registry['cmds']:
self.app.registry['cmds'][cmd_cls.SHORT_NAME] = cmd_cls(self.app.config)
return self.app.registry['cmds'][cmd_cls.SHORT_NAME]
return ('/admin/command/' + cmd_cls.SHORT_NAME, H)
示例2: _build_state_value
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def _build_state_value(request_handler, user):
"""Composes the value for the 'state' parameter.
Packs the current request URI and an XSRF token into an opaque string that
can be passed to the authentication server via the 'state' parameter.
Args:
request_handler: webapp.RequestHandler, The request.
user: google.appengine.api.users.User, The current user.
Returns:
The state value as a string.
"""
uri = request_handler.request.url
token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
action_id=str(uri))
return uri + ':' + token
示例3: _create_flow
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def _create_flow(self, request_handler):
"""Create the Flow object.
The Flow is calculated lazily since we don't know where this app is
running until it receives a request, at which point redirect_uri can be
calculated and then the Flow object can be constructed.
Args:
request_handler: webapp.RequestHandler, the request handler.
"""
if self.flow is None:
redirect_uri = request_handler.request.relative_url(
self._callback_path) # Usually /oauth2callback
self.flow = OAuth2WebServerFlow(
self._client_id, self._client_secret, self._scope,
redirect_uri=redirect_uri, user_agent=self._user_agent,
auth_uri=self._auth_uri, token_uri=self._token_uri,
revoke_uri=self._revoke_uri, **self._kwargs)
示例4: _create_flow
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def _create_flow(self, request_handler):
"""Create the Flow object.
The Flow is calculated lazily since we don't know where this app is
running until it receives a request, at which point redirect_uri can be
calculated and then the Flow object can be constructed.
Args:
request_handler: webapp.RequestHandler, the request handler.
"""
if self.flow is None:
redirect_uri = request_handler.request.relative_url(
self._callback_path) # Usually /oauth2callback
self.flow = client.OAuth2WebServerFlow(
self._client_id, self._client_secret, self._scope,
redirect_uri=redirect_uri, user_agent=self._user_agent,
auth_uri=self._auth_uri, token_uri=self._token_uri,
revoke_uri=self._revoke_uri, **self._kwargs)
示例5: forbid_ui_on_replica
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def forbid_ui_on_replica(method):
"""Decorator for methods that are not allowed to be called on Replica.
If such method is called on a service in Replica mode, it would return
HTTP 405 "Method Not Allowed".
"""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
assert isinstance(self, webapp2.RequestHandler)
if model.is_replica():
primary_url = model.get_replication_state().primary_url
self.abort(
405,
detail='Not allowed on a replica, see primary at %s' % primary_url)
return method(self, *args, **kwargs)
return wrapper
示例6: forbid_api_on_replica
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def forbid_api_on_replica(method):
"""Decorator for methods that are not allowed to be called on Replica.
If such method is called on a service in Replica mode, it would return
HTTP 405 "Method Not Allowed".
"""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
assert isinstance(self, webapp2.RequestHandler)
if model.is_replica():
self.abort(
405,
json={
'primary_url': model.get_replication_state().primary_url,
'text': 'Use Primary service for API requests',
},
headers={
'Content-Type': 'application/json; charset=utf-8',
})
return method(self, *args, **kwargs)
return wrapper
示例7: test_forbidden_on_replica
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def test_forbidden_on_replica(self):
calls = []
class Handler(webapp2.RequestHandler):
@rest_api.forbid_api_on_replica
def get(self):
calls.append(1)
mock_replication_state('http://locahost:1234')
response = call_get(Handler, status=405)
self.assertEqual(0, len(calls))
expected = {
'primary_url': 'http://locahost:1234',
'text': 'Use Primary service for API requests',
}
self.assertEqual(expected, json.loads(response.body))
示例8: directory_handler_factory
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def directory_handler_factory(api_classes, base_path):
"""Returns a directory request handler which knows about the given services.
Args:
api_classes: A list of protorpc.remote.Service classes the handler should
know about.
base_path: The base path under which all service paths exist.
Returns:
A webapp2.RequestHandler.
"""
class DirectoryHandler(webapp2.RequestHandler):
"""Returns a directory list for known services."""
def get(self):
host = self.request.headers['Host']
self.response.headers['Content-Type'] = 'application/json'
json.dump(
discovery.directory(api_classes, host, base_path),
self.response, indent=2, sort_keys=True, separators=(',', ':'))
return DirectoryHandler
示例9: explorer_redirect_route
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def explorer_redirect_route(base_path):
"""Returns a route to a handler which redirects to the API explorer.
Args:
base_path: The base path under which all service paths exist.
Returns:
A webapp2.Route.
"""
class RedirectHandler(webapp2.RequestHandler):
"""Returns a handler redirecting to the API explorer."""
def get(self):
host = self.request.headers['Host']
self.redirect('https://apis-explorer.appspot.com/apis-explorer'
'/?base=https://%s%s' % (host, base_path))
return webapp2.Route('%s/explorer' % base_path, RedirectHandler)
示例10: redirect_ui_on_replica
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def redirect_ui_on_replica(method):
"""Decorator for methods that redirect to Primary when called on replica.
If such method is called on a service in Replica mode, it would return
HTTP 302 redirect to corresponding method on Primary.
"""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
assert isinstance(self, webapp2.RequestHandler)
assert self.request.method == 'GET'
if model.is_replica():
primary_url = model.get_replication_state().primary_url
protocol = 'http://' if utils.is_local_dev_server() else 'https://'
assert primary_url and primary_url.startswith(protocol), primary_url
assert self.request.path_qs.startswith('/'), self.request.path_qs
self.redirect(primary_url.rstrip('/') + self.request.path_qs, abort=True)
return method(self, *args, **kwargs)
return wrapper
################################################################################
## Admin routes. The use cookies and GAE's "is_current_user_admin" for authn.
示例11: explorer_proxy_route
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def explorer_proxy_route(base_path):
"""Returns a route to a handler which serves an API explorer proxy.
Args:
base_path: The base path under which all service paths exist.
Returns:
A webapp2.Route.
"""
class ProxyHandler(webapp2.RequestHandler):
"""Returns a proxy capable of handling requests from API explorer."""
def get(self):
self.response.write(template.render(
'adapter/proxy.html', params={'base_path': base_path}))
template.bootstrap({
'adapter': os.path.join(THIS_DIR, 'templates'),
})
return webapp2.Route('%s/static/proxy.html' % base_path, ProxyHandler)
示例12: __init__
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def __init__(self, *args, **kwargs):
"""Override RequestHandler init to track app readiness."""
super(FrontendHandler, self).__init__(*args, **kwargs)
self.bootstrap_started = bootstrap.is_bootstrap_started()
self.bootstrap_completed = bootstrap.is_bootstrap_completed()
示例13: dispatch
# 需要导入模块: import webapp2 [as 别名]
# 或者: from webapp2 import RequestHandler [as 别名]
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)