本文整理匯總了Python中werkzeug.wrappers.BaseResponse方法的典型用法代碼示例。如果您正苦於以下問題:Python wrappers.BaseResponse方法的具體用法?Python wrappers.BaseResponse怎麽用?Python wrappers.BaseResponse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類werkzeug.wrappers
的用法示例。
在下文中一共展示了wrappers.BaseResponse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: application
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def application(cls, f):
"""Decorate a function as responder that accepts the request as first
argument. This works like the :func:`responder` decorator but the
function is passed the request object as first argument and the
request object will be closed automatically::
@Request.application
def my_wsgi_app(request):
return Response('Hello World!')
:param f: the WSGI callable to decorate
:return: a new WSGI callable
"""
#: return a callable that wraps the -2nd argument with the request
#: and calls the function with all the arguments up to that one and
#: the request. The return value is then called with the latest
#: two arguments. This makes it possible to use this decorator for
#: both methods and standalone WSGI functions.
def application(*args):
request = cls(args[-2])
with request:
return f(*args[:-2] + (request,))(*args[-2:])
return update_wrapper(application, f)
示例2: test_app
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def test_app(environ, start_response):
"""Simple test application that dumps the environment. You can use
it to check if Werkzeug is working properly:
.. sourcecode:: pycon
>>> from werkzeug.serving import run_simple
>>> from werkzeug.testapp import test_app
>>> run_simple('localhost', 3000, test_app)
* Running on http://localhost:3000/
The application displays important information from the WSGI environment,
the Python interpreter and the installed libraries.
"""
req = Request(environ, populate_request=False)
if req.args.get('resource') == 'logo':
response = logo
else:
response = Response(render_testapp(req), mimetype='text/html')
return response(environ, start_response)
示例3: _error_handler__
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def _error_handler__(cls, fn, e):
"""
Error handler callback.
adding _error_handler() or _error_$code()/_error_404(),
will trigger this method to return the response
The method must accept one arg, which is the error object
'self' can still be used in your class
:param fn: the method invoked
:param e: the error object
"""
resp = fn(cls, e)
if isinstance(resp, Response) or isinstance(resp, BaseResponse):
return resp
if isinstance(resp, dict) or isinstance(resp, tuple) or resp is None:
data, status, headers = utils.prepare_view_response(resp)
# create template from the error name, without the leading _,
# ie: _error_handler -> error_handler.html, _error_404 -> error_404.html
# template can be changed using @respone.template('app/class/action.html')
if "__template__" not in data:
data["__template__"] = _make_template_path(cls, fn.__name__.lstrip("_"))
return cls.render(**resp), e.code, headers
return resp
# ------------------------------------------------------------------------------
示例4: test_flow
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def test_flow(self, satosa_config_dict):
"""
Performs the test.
"""
test_client = Client(make_app(SATOSAConfig(satosa_config_dict)), BaseResponse)
# Make request to frontend
resp = test_client.get('/{}/{}/request'.format("backend", "frontend"))
assert resp.status == '200 OK'
headers = dict(resp.headers)
assert headers["Set-Cookie"]
# Fake response coming in to backend
resp = test_client.get('/{}/response'.format("backend"), headers=[("Cookie", headers["Set-Cookie"])])
assert resp.status == '200 OK'
assert resp.data.decode('utf-8') == "Auth response received, passed to test frontend"
示例5: test_full_path
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def test_full_path(self):
trace_id = trace_sdk.generate_trace_id()
# We need to use the Werkzeug test app because
# The headers are injected at the wsgi layer.
# The flask test app will not include these, and
# result in the values not propagated.
client = Client(self.app.wsgi_app, BaseResponse)
# emulate b3 headers
client.get(
"/",
headers={
"traceparent": "00-{:032x}-{:016x}-{:02x}".format(
trace_id,
trace_sdk.generate_span_id(),
trace.TraceFlags.SAMPLED,
)
},
)
# assert the http request header was propagated through.
prepared_request = self.send.call_args[0][1]
headers = prepared_request.headers
self.assertRegex(
headers["traceparent"],
r"00-{:032x}-[0-9a-f]{{16}}-01".format(trace_id),
)
示例6: test_uninstrument
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def test_uninstrument(self):
# pylint: disable=access-member-before-definition
resp = self.client.get("/hello/123")
self.assertEqual(200, resp.status_code)
self.assertEqual([b"Hello: 123"], list(resp.response))
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
FlaskInstrumentor().uninstrument()
self.app = flask.Flask(__name__)
self.app.route("/hello/<int:helloid>")(self._hello_endpoint)
# pylint: disable=attribute-defined-outside-init
self.client = Client(self.app, BaseResponse)
resp = self.client.get("/hello/123")
self.assertEqual(200, resp.status_code)
self.assertEqual([b"Hello: 123"], list(resp.response))
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
示例7: _common_initialization
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def _common_initialization(self, config):
# pylint: disable=unused-argument
def excluded_endpoint(request):
return Response("excluded")
# pylint: disable=unused-argument
def excluded2_endpoint(request):
return Response("excluded2")
config.add_route("hello", "/hello/{helloid}")
config.add_view(self._hello_endpoint, route_name="hello")
config.add_route("excluded_arg", "/excluded/{helloid}")
config.add_view(self._hello_endpoint, route_name="excluded_arg")
config.add_route("excluded", "/excluded_noarg")
config.add_view(excluded_endpoint, route_name="excluded")
config.add_route("excluded2", "/excluded_noarg2")
config.add_view(excluded2_endpoint, route_name="excluded2")
# pylint: disable=attribute-defined-outside-init
self.client = Client(config.make_wsgi_app(), BaseResponse)
示例8: _warn_if_string
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def _warn_if_string(iterable):
"""Helper for the response objects to check if the iterable returned
to the WSGI server is not a string.
"""
if isinstance(iterable, string_types):
warnings.warn(
"Response iterable was set to a string. This will appear to"
" work but means that the server will send the data to the"
" client one character at a time. This is almost never"
" intended behavior, use 'response.data' to assign strings"
" to the response object.",
stacklevel=2,
)
示例9: execute_command
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def execute_command(self, request, command, frame):
"""Execute a command in a console."""
return Response(frame.console.eval(command), mimetype='text/html')
示例10: display_console
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def display_console(self, request):
"""Display a standalone shell."""
if 0 not in self.frames:
if self.console_init_func is None:
ns = {}
else:
ns = dict(self.console_init_func())
ns.setdefault('app', self.app)
self.frames[0] = _ConsoleFrame(ns)
is_trusted = bool(self.check_pin_trust(request.environ))
return Response(render_console_html(secret=self.secret,
evalex_trusted=is_trusted),
mimetype='text/html')
示例11: paste_traceback
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def paste_traceback(self, request, traceback):
"""Paste the traceback and return a JSON response."""
rv = traceback.paste()
return Response(json.dumps(rv), mimetype='application/json')
示例12: get_resource
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def get_resource(self, request, filename):
"""Return a static resource from the shared folder."""
filename = join(dirname(__file__), 'shared', basename(filename))
if isfile(filename):
mimetype = mimetypes.guess_type(filename)[0] \
or 'application/octet-stream'
f = open(filename, 'rb')
try:
return Response(f.read(), mimetype=mimetype)
finally:
f.close()
return Response('Not Found', status=404)
示例13: get_response
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def get_response(self):
"""Return a response object for the feed."""
return BaseResponse(self.to_string(), mimetype='application/atom+xml')
示例14: application
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def application(cls, f):
"""Decorate a function as responder that accepts the request as first
argument. This works like the :func:`responder` decorator but the
function is passed the request object as first argument and the
request object will be closed automatically::
@Request.application
def my_wsgi_app(request):
return Response('Hello World!')
As of Werkzeug 0.14 HTTP exceptions are automatically caught and
converted to responses instead of failing.
:param f: the WSGI callable to decorate
:return: a new WSGI callable
"""
#: return a callable that wraps the -2nd argument with the request
#: and calls the function with all the arguments up to that one and
#: the request. The return value is then called with the latest
#: two arguments. This makes it possible to use this decorator for
#: both methods and standalone WSGI functions.
from werkzeug.exceptions import HTTPException
def application(*args):
request = cls(args[-2])
with request:
try:
resp = f(*args[:-2] + (request,))
except HTTPException as e:
resp = e.get_response(args[-2])
return resp(*args[-2:])
return update_wrapper(application, f)
示例15: _wrap_response
# 需要導入模塊: from werkzeug import wrappers [as 別名]
# 或者: from werkzeug.wrappers import BaseResponse [as 別名]
def _wrap_response(self, start, length):
"""Wrap existing Response in case of Range Request context."""
if self.status_code == 206:
self.response = _RangeWrapper(self.response, start, length)