本文整理汇总了Python中twisted.web.error.UnsupportedMethod方法的典型用法代码示例。如果您正苦于以下问题:Python error.UnsupportedMethod方法的具体用法?Python error.UnsupportedMethod怎么用?Python error.UnsupportedMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.error
的用法示例。
在下文中一共展示了error.UnsupportedMethod方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_str
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def test_str(self):
"""
The C{__str__} for L{UnsupportedMethod} makes it clear that what it
shows is a list of the supported methods, not the method that was
unsupported.
"""
b = "b" if _PY3 else ""
e = error.UnsupportedMethod([b"HEAD", b"PATCH"])
self.assertEqual(
str(e), "Expected one of [{b}'HEAD', {b}'PATCH']".format(b=b),
)
示例2: test_explicitAllowedMethods
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def test_explicitAllowedMethods(self):
"""
The L{UnsupportedMethod} raised by L{Resource.render} for an unsupported
request method has a C{allowedMethods} attribute set to the value of the
C{allowedMethods} attribute of the L{Resource}, if it has one.
"""
expected = [b'GET', b'HEAD', b'PUT']
resource = Resource()
resource.allowedMethods = expected
request = DummyRequest([])
request.method = b'FICTIONAL'
exc = self.assertRaises(UnsupportedMethod, resource.render, request)
self.assertEqual(set(expected), set(exc.allowedMethods))
示例3: test_implicitAllowedMethods
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def test_implicitAllowedMethods(self):
"""
The L{UnsupportedMethod} raised by L{Resource.render} for an unsupported
request method has a C{allowedMethods} attribute set to a list of the
methods supported by the L{Resource}, as determined by the
I{render_}-prefixed methods which it defines, if C{allowedMethods} is
not explicitly defined by the L{Resource}.
"""
expected = set([b'GET', b'HEAD', b'PUT'])
resource = ImplicitAllowedMethods()
request = DummyRequest([])
request.method = b'FICTIONAL'
exc = self.assertRaises(UnsupportedMethod, resource.render, request)
self.assertEqual(expected, set(exc.allowedMethods))
示例4: render
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def render(self, request):
"""
Leave the request open for future writes.
"""
self.request = request
if request.method not in self.allowedMethods:
raise error.UnsupportedMethod(self.allowedMethods)
self.request.write(b"some data")
return server.NOT_DONE_YET
示例5: render
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def render(request):
"""
Render a request. This is called on the leaf resource for a request.
@return: Either C{server.NOT_DONE_YET} to indicate an asynchronous or a
C{bytes} instance to write as the response to the request. If
C{NOT_DONE_YET} is returned, at some point later (for example, in a
Deferred callback) call C{request.write(b"<html>")} to write data to
the request, and C{request.finish()} to send the data to the
browser.
@raise twisted.web.error.UnsupportedMethod: If the HTTP verb
requested is not supported by this resource.
"""
示例6: _computeAllowedMethods
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def _computeAllowedMethods(resource):
"""
Compute the allowed methods on a C{Resource} based on defined render_FOO
methods. Used when raising C{UnsupportedMethod} but C{Resource} does
not define C{allowedMethods} attribute.
"""
allowedMethods = []
for name in prefixedMethodNames(resource.__class__, "render_"):
# Potentially there should be an API for encode('ascii') in this
# situation - an API for taking a Python native string (bytes on Python
# 2, text on Python 3) and returning a socket-compatible string type.
allowedMethods.append(name.encode('ascii'))
return allowedMethods
示例7: render
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def render(self, request):
"""
Render a given resource. See L{IResource}'s render method.
I delegate to methods of self with the form 'render_METHOD'
where METHOD is the HTTP that was used to make the
request. Examples: render_GET, render_HEAD, render_POST, and
so on. Generally you should implement those methods instead of
overriding this one.
render_METHOD methods are expected to return a string which
will be the rendered page, unless the return value is
twisted.web.server.NOT_DONE_YET, in which case it is this
class's responsibility to write the results to
request.write(data), then call request.finish().
Old code that overrides render() directly is likewise expected
to return a string or NOT_DONE_YET.
"""
m = getattr(self, 'render_' + request.method, None)
if not m:
# This needs to be here until the deprecated subclasses of the
# below three error resources in twisted.web.error are removed.
from twisted.web.error import UnsupportedMethod
raise UnsupportedMethod(getattr(self, 'allowedMethods', ()))
return m(request)
示例8: render
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def render(self, request):
raise UnsupportedMethod(())
示例9: test_get_is_not_supported_for_logout
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def test_get_is_not_supported_for_logout(self):
request = DummyRequest(['/logout'])
request.method = 'GET'
self.assertRaises(UnsupportedMethod, self.web.get, request)
示例10: test_getResourceFor_wraps_render_wo_underlay_raises_no_method
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def test_getResourceFor_wraps_render_wo_underlay_raises_no_method(self):
root = Resource()
maas = Resource()
root.putChild(b"MAAS", maas)
site = OverlaySite(root)
request = DummyRequest([b"MAAS"])
resource = site.getResourceFor(request)
self.assertThat(resource, Is(maas))
self.assertRaises(UnsupportedMethod, resource.render, request)
示例11: render
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def render(self, resrc):
try:
body = resrc.render(self)
except UnsupportedMethod, e:
allowedMethods = e.allowedMethods
if (self.method == "HEAD") and ("GET" in allowedMethods):
# We must support HEAD (RFC 2616, 5.1.1). If the
# resource doesn't, fake it by giving the resource
# a 'GET' request and then return only the headers,
# not the body.
log.msg("Using GET to fake a HEAD request for %s" %
(resrc,))
self.method = "GET"
body = resrc.render(self)
if body is NOT_DONE_YET:
log.msg("Tried to fake a HEAD request for %s, but "
"it got away from me." % resrc)
# Oh well, I guess we won't include the content length.
else:
self.setHeader('content-length', str(len(body)))
self.write('')
self.finish()
return
if self.method in (supportedMethods):
# We MUST include an Allow header
# (RFC 2616, 10.4.6 and 14.7)
self.setHeader('Allow', allowedMethods)
s = ('''Your browser approached me (at %(URI)s) with'''
''' the method "%(method)s". I only allow'''
''' the method%(plural)s %(allowed)s here.''' % {
'URI': self.uri,
'method': self.method,
'plural': ((len(allowedMethods) > 1) and 's') or '',
'allowed': string.join(allowedMethods, ', ')
})
epage = resource.ErrorPage(http.NOT_ALLOWED,
"Method Not Allowed", s)
body = epage.render(self)
else:
epage = resource.ErrorPage(http.NOT_IMPLEMENTED, "Huh?",
"I don't know how to treat a"
" %s request." % (self.method,))
body = epage.render(self)
# end except UnsupportedMethod
示例12: getResourceFor
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import UnsupportedMethod [as 别名]
def getResourceFor(self, request):
"""Override to support an underlay site.
If this site cannot return a valid resource to request is passed to
the underlay site to resolve the request.
"""
def call_underlay(request):
# Reset the paths and forward to the underlay site.
request.prepath = []
request.postpath = postpath
return self.underlay.getResourceFor(request)
def wrap_render(orig_render, request):
# Wrap the render call of the resource, catching any
# UnsupportedMethod exceptions and forwarding those onto
# the underlay site.
try:
return orig_render(request)
except UnsupportedMethod:
if self.underlay is not None:
resource = call_underlay(request)
return resource.render(request)
else:
raise
postpath = copy.copy(request.postpath)
result = super().getResourceFor(request)
if isinstance(result, NoResource) and self.underlay is not None:
return call_underlay(request)
else:
if hasattr(result.render, "__overlay_wrapped__"):
# Render method for the resulting resource has already been
# wrapped, so don't wrap it again.
return result
else:
# First time this resource has been returned. Wrap the render
# method so if the resource doesn't support that method
# it will be passed to the underlay.
result.render = partial(wrap_render, result.render)
result.render.__overlay_wrapped__ = True
return result