本文整理汇总了Python中werkzeug.wrappers.BaseResponse类的典型用法代码示例。如果您正苦于以下问题:Python BaseResponse类的具体用法?Python BaseResponse怎么用?Python BaseResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BaseResponse类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: redirect
def redirect(location, code=302):
"""Return a response object (a WSGI application) that, if called,
redirects the client to the target location. Supported codes are 301,
302, 303, 305, and 307. 300 is not supported because it's not a real
redirect and 304 because it's the answer for a request with a request
with defined If-Modified-Since headers.
.. versionadded:: 0.6
The location can now be unicode strings that are encoded using
the :func:`iri_to_uri` function.
:param location: the location the response should redirect to.
:param code: the redirect status code.
"""
assert code in (301, 302, 303, 305, 307), 'invalid code'
from werkzeug.wrappers import BaseResponse
display_location = location
if isinstance(location, unicode):
from werkzeug.urls import iri_to_uri
location = iri_to_uri(location)
response = BaseResponse(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n'
'<title>Redirecting...</title>\n'
'<h1>Redirecting...</h1>\n'
'<p>You should be redirected automatically to target URL: '
'<a href="%s">%s</a>. If not click the link.' %
(location, display_location), code, mimetype='text/html')
response.headers['Location'] = location
return response
示例2: redirect
def redirect(location, code = 302):
from werkzeug.wrappers import BaseResponse
display_location = location
if isinstance(location, unicode):
from werkzeug.urls import iri_to_uri
location = iri_to_uri(location)
response = BaseResponse('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>Redirecting...</title>\n<h1>Redirecting...</h1>\n<p>You should be redirected automatically to target URL: <a href="%s">%s</a>. If not click the link.' % (location, display_location), code, mimetype='text/html')
response.headers['Location'] = location
return response
示例3: __init__
def __init__(
self, response=None, status=200, headers=None, mimetype=None, content_type=None
):
if isinstance(response, Stream):
response = response.render("html", encoding=None, doctype="html")
BaseResponse.__init__(self, response, status, headers, mimetype, content_type)
示例4: pin_auth
def pin_auth(self, request):
"""Authenticates with the pin."""
exhausted = False
auth = False
if self.is_trusted(request.environ):
auth = True
elif self._failed_pin_auth > 10:
exhausted = True
else:
entered_pin = request.args.get('pin')
if entered_pin.strip().replace('-', '') == \
self.pin.replace('-', ''):
self._failed_pin_auth = 0
auth = True
else:
time.sleep(self._failed_pin_auth > 5 and 5.0 or 0.5)
self._failed_pin_auth += 1
auth = False
rv = Response(json.dumps({
'auth': auth,
'exhausted': exhausted,
}), mimetype='application/json')
if auth:
rv.set_cookie(self.pin_cookie_name, str(int(time.time())),
httponly=True)
return rv
示例5: __common_resp_handle
def __common_resp_handle(data, error, status):
"""
common response handling:
- add common response headers
- serialize response
@data must be json serializable
@error will be serialized with str()
"""
def __response_wrap(data=None, error=None):
"""
wrap response data/errors as dict - this should always be used when returning
data to allow easy return of list objects, assist in error case distinction, etc.
"""
return dict(data=data, error=error)
if not error:
error_str = None
else:
error_str = str(error) # convert any Exception objects to serializable form
ret_data = __response_wrap(data, error_str)
resp_payload = json.dumps(ret_data)
resp = Response(resp_payload, mimetype='application/json', status=status)
resp.headers['Access-Control-Allow-Origin'] = '*'
# more response processing
return resp
示例6: pin_auth
def pin_auth(self, request):
"""Authenticates with the pin."""
exhausted = False
auth = False
trust = self.check_pin_trust(request.environ)
# If the trust return value is `None` it means that the cookie is
# set but the stored pin hash value is bad. This means that the
# pin was changed. In this case we count a bad auth and unset the
# cookie. This way it becomes harder to guess the cookie name
# instead of the pin as we still count up failures.
bad_cookie = False
if trust is None:
self._fail_pin_auth()
bad_cookie = True
# If we're trusted, we're authenticated.
elif trust:
auth = True
# If we failed too many times, then we're locked out.
elif self._failed_pin_auth > 10:
exhausted = True
# Otherwise go through pin based authentication
else:
entered_pin = request.args.get('pin')
if entered_pin.strip().replace('-', '') == \
self.pin.replace('-', ''):
self._failed_pin_auth = 0
auth = True
else:
self._fail_pin_auth()
rv = Response(json.dumps({
'auth': auth,
'exhausted': exhausted,
}), mimetype='application/json')
if auth:
rv.set_cookie(self.pin_cookie_name, '%s|%s' % (
int(time.time()),
hash_pin(self.pin)
), httponly=True)
elif bad_cookie:
rv.delete_cookie(self.pin_cookie_name)
return rv