本文整理汇总了Python中twisted.web.resource.NoResource方法的典型用法代码示例。如果您正苦于以下问题:Python resource.NoResource方法的具体用法?Python resource.NoResource怎么用?Python resource.NoResource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.resource
的用法示例。
在下文中一共展示了resource.NoResource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def render(self, request):
"""Render me to a web client.
Load my file, execute it in a special namespace (with 'request' and
'__file__' global vars) and finish the request. Output to the web-page
will NOT be handled with print - standard output goes to the log - but
with request.write.
"""
request.setHeader("x-powered-by","Twisted/%s" % copyright.version)
namespace = {'request': request,
'__file__': self.filename,
'registry': self.registry}
try:
execfile(self.filename, namespace, namespace)
except IOError, e:
if e.errno == 2: #file not found
request.setResponseCode(http.NOT_FOUND)
request.write(resource.NoResource("File not found.").render(request))
示例2: getChild
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def getChild(self, path, request):
fn = os.path.join(self.path, path)
if os.path.isdir(fn):
return ResourceScriptDirectory(fn, self.registry)
if os.path.exists(fn):
return ResourceScript(fn, self.registry)
return resource.NoResource()
示例3: render
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def render(self, request):
return resource.NoResource().render(request)
示例4: getChild
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def getChild(self, name, request):
if name == '':
return self
td = '.twistd'
if name[-len(td):] == td:
username = name[:-len(td)]
sub = 1
else:
username = name
sub = 0
try:
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell \
= self._pwd.getpwnam(username)
except KeyError:
return resource.NoResource()
if sub:
twistdsock = os.path.join(pw_dir, self.userSocketName)
rs = ResourceSubscription('unix',twistdsock)
self.putChild(name, rs)
return rs
else:
path = os.path.join(pw_dir, self.userDirName)
if not os.path.exists(path):
return resource.NoResource()
return static.File(path)
示例5: _getResourceForRequest
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def _getResourceForRequest(self, request):
"""(Internal) Get the appropriate resource for the given host.
"""
hostHeader = request.getHeader(b'host')
if hostHeader == None:
return self.default or resource.NoResource()
else:
host = hostHeader.lower().split(b':', 1)[0]
return (self.hosts.get(host, self.default)
or resource.NoResource("host %s not in vhost map" % repr(host)))
示例6: render
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def render(self, request):
notFound = resource.NoResource(
"CGI directories do not support directory listing.")
return notFound.render(request)
示例7: test_getChild
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def test_getChild(self):
"""
L{_HostResource.getChild} returns the proper I{Resource} for the vhost
embedded in the URL. Verify that returning the proper I{Resource}
required changing the I{Host} in the header.
"""
bazroot = Data(b'root data', "")
bazuri = Data(b'uri data', "")
baztest = Data(b'test data', "")
bazuri.putChild(b'test', baztest)
bazroot.putChild(b'uri', bazuri)
hr = _HostResource()
root = NameVirtualHost()
root.default = Data(b'default data', "")
root.addHost(b'baz.com', bazroot)
request = DummyRequest([b'uri', b'test'])
request.prepath = [b'bar', b'http', b'baz.com']
request.site = Site(root)
request.isSecure = lambda: False
request.host = b''
step = hr.getChild(b'baz.com', request) # Consumes rest of path
self.assertIsInstance(step, Data)
request = DummyRequest([b'uri', b'test'])
step = root.getChild(b'uri', request)
self.assertIsInstance(step, NoResource)
示例8: __init__
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def __init__(self, *args, **kwargs):
warnings.warn(
"twisted.web.error.NoResource is deprecated since Twisted 9.0. "
"See twisted.web.resource.NoResource.", DeprecationWarning,
stacklevel=2)
_resource.NoResource.__init__(self, *args, **kwargs)
示例9: getChild
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def getChild(self, path, request):
fnp = self.child(path)
if not fnp.exists():
return static.File.childNotFound
elif fnp.isdir():
return CGIDirectory(fnp.path)
else:
return CGIScript(fnp.path)
return resource.NoResource()
示例10: test_noResourceRendering
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def test_noResourceRendering(self):
"""
L{NoResource} sets the HTTP I{NOT FOUND} code.
"""
detail = "long message"
page = self.noResource(detail)
self._pageRenderingTest(page, NOT_FOUND, "No Such Resource", detail)
示例11: noResource
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def noResource(self, *args):
return error.NoResource(*args)
示例12: getChild
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def getChild(self, path, request):
if path == '':
return self
if path == 'login':
return self
if path == 'status':
return LoginStatusResource(self._services_factory)
if path == AccountRecoveryResource.BASE_URL:
return AccountRecoveryResource(self._services_factory)
if not self.is_logged_in(request):
return UnAuthorizedResource()
return NoResource()
示例13: get
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def get(self, path):
return self._registry.get(path) or NoResource()
示例14: test_getResourceFor_returns_no_resource_wo_underlay
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def test_getResourceFor_returns_no_resource_wo_underlay(self):
root = Resource()
site = OverlaySite(root)
request = DummyRequest([b"MAAS"])
resource = site.getResourceFor(request)
self.assertThat(resource, IsInstance(NoResource))
示例15: get_image
# 需要导入模块: from twisted.web import resource [as 别名]
# 或者: from twisted.web.resource import NoResource [as 别名]
def get_image(self, request):
@defer.inlineCallbacks
def _showImage(resp=None):
@defer.inlineCallbacks
def _setContentDispositionAndSend(file_path, extension, content_type):
request.setHeader('content-disposition', 'filename="%s.%s"' % (file_path, extension))
request.setHeader('content-type', content_type)
request.setHeader('cache-control', 'max-age=604800')
f = open(file_path, "rb")
yield FileSender().beginFileTransfer(f, request)
f.close()
defer.returnValue(0)
if os.path.exists(image_path):
yield _setContentDispositionAndSend(image_path, "jpg", "image/jpeg")
else:
request.setResponseCode(http.NOT_FOUND)
request.write("No such image '%s'" % request.path)
request.finish()
if "hash" in request.args and len(request.args["hash"][0]) == 40:
if self.db.filemap.get_file(request.args["hash"][0]) is not None:
image_path = self.db.filemap.get_file(request.args["hash"][0])
else:
image_path = os.path.join(DATA_FOLDER, "cache", request.args["hash"][0])
if not os.path.exists(image_path) and "guid" in request.args:
node = None
for connection in self.protocol.values():
if connection.handler.node is not None and \
connection.handler.node.id == unhexlify(request.args["guid"][0]):
node = connection.handler.node
self.mserver.get_image(node, unhexlify(request.args["hash"][0])).addCallback(_showImage)
if node is None:
_showImage()
else:
_showImage()
else:
request.write(NoResource().render(request))
request.finish()
return server.NOT_DONE_YET