本文整理汇总了Python中twisted.web.static.Data.putChild方法的典型用法代码示例。如果您正苦于以下问题:Python Data.putChild方法的具体用法?Python Data.putChild怎么用?Python Data.putChild使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.static.Data
的用法示例。
在下文中一共展示了Data.putChild方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
def setUp(self):
root = Data(b'El toro!', 'text/plain')
root.putChild(b"cookiemirror", CookieMirrorResource())
root.putChild(b"rawcookiemirror", RawCookieMirrorResource())
site = server.Site(root, timeout=None)
self.port = self._listen(site)
self.portno = self.port.getHost().port
示例2: test_getChild
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [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)
示例3: go
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
def go(self, reactor):
data = Data("Hello world\n", "text/plain")
data.putChild("", data)
factory = Site(data)
# TODO: adoptStreamConnection should really support AF_UNIX
protocol = ConnectionFromManager(reactor, factory)
skt = fromfd(MAGIC_FILE_DESCRIPTOR, AF_UNIX, SOCK_STREAM)
os.close(MAGIC_FILE_DESCRIPTOR)
serverTransport = UNIXServer(skt, protocol, None, None, 1234, reactor)
protocol.makeConnection(serverTransport)
serverTransport.startReading()
globalLogBeginner.beginLoggingTo([protocol.sendLog])
factory.doStart()
return Deferred()
示例4: test_resource_tree
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
def test_resource_tree(self):
"""
The agent should traverse the resource tree correctly
"""
class R(Resource):
def __init__(self, response):
Resource.__init__(self)
self._response = response
def render_GET(self, request):
return self._response
a = Data('a', 'text/plain')
c = Data('c', 'text/plain')
a.putChild('b', Data('b', 'text/plain'))
a.putChild('c', c)
c.putChild('d', Data('d', 'text/plain'))
agent = self.getAgent(a)
# are these assertions correct?
yield self.assertBody(agent, 'a', 'GET', 'http://example.com')
yield self.assertBody(agent, 'a', 'GET', 'http://example.com/')
yield self.assertBody(agent, 'b', 'GET', 'http://example.com/b')
yield self.assertBody(agent, 'c', 'GET', 'http://example.com/c')
yield self.assertBody(agent, 'd', 'GET', 'http://example.com/c/d')
示例5: get_provider_factory
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
def get_provider_factory():
"""
Instantiates a Site that serves the resources
that we expect from a valid provider.
Listens on:
* port 8000 for http connections
* port 8443 for https connections
:rparam: factory for a site
:rtype: Site instance
"""
root = Data("", "")
root.putChild("", root)
root.putChild("provider.json", FileModified(
os.path.join(_here,
"test_provider.json")))
config = Resource()
config.putChild(
"eip-service.json",
FileModified(
os.path.join(_here, "eip-service.json")))
apiv1 = Resource()
apiv1.putChild("config", config)
apiv1.putChild("sessions", API_Sessions())
apiv1.putChild("users", FakeUsers(None))
apiv1.putChild("cert", FileModified(
os.path.join(_here,
'openvpn.pem')))
root.putChild("1", apiv1)
factory = Site(root)
return factory
示例6: setUp
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
def setUp(self):
self.avatar_content = b"avatar content"
self.child_content = b"child content"
self.grandchild_content = b"grandchild content"
grandchild = Data(self.grandchild_content, b"text/plain")
child = Data(self.child_content, b"text/plain")
child.putChild(b"grandchild", grandchild)
self.avatar = Data(self.avatar_content, b"text/plain")
self.avatar.putChild(b"child", child)
self.realm = OneIResourceAvatarRealm(self.avatar)
self.portal = Portal(
self.realm,
[AllowAnonymousAccess()],
)
self.guard = HTTPAuthSessionWrapper(
self.portal,
[BasicCredentialFactory("example.com")],
)
示例7: len
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
self.sendClose()
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
log.startLogging(sys.stdout)
debug = True
else:
debug = False
factorySave = WebSocketServerFactory("ws://localhost:{0}".format(LOCAL_PROXY_PORT), debug = debug)
factorySave.protocol = RemoteControlSaveProtocol
resourceSave = WebSocketResource(factorySave)
factoryLoad = WebSocketServerFactory("ws://localhost:{0}".format(LOCAL_PROXY_PORT), debug = debug)
factoryLoad.protocol = RemoteControlLoadProtocol
resourceLoad = WebSocketResource(factoryLoad)
# Establish a dummy root resource
root = Data("", "text/plain")
# and our WebSocket servers under different paths ..
root.putChild("save", resourceSave)
root.putChild("load", resourceLoad)
# both under one Twisted Web Site
site = Site(root)
reactor.listenTCP(LOCAL_PROXY_PORT, site)
reactor.run()
示例8: len
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
log.startLogging(sys.stdout)
debug = True
else:
debug = False
factory1 = WebSocketServerFactory("ws://localhost:9000",
debug = debug,
debugCodePaths = debug)
factory1.protocol = Echo1ServerProtocol
resource1 = WebSocketResource(factory1)
factory2 = WebSocketServerFactory("ws://localhost:9000",
debug = debug,
debugCodePaths = debug)
factory2.protocol = Echo2ServerProtocol
resource2 = WebSocketResource(factory2)
## Establish a dummy root resource
root = Data("", "text/plain")
## and our WebSocket servers under different paths ..
root.putChild("echo1", resource1)
root.putChild("echo2", resource2)
## both under one Twisted Web Site
site = Site(root)
reactor.listenTCP(9000, site)
reactor.run()
示例9: GuardTests
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
class GuardTests(TestCase):
"""
Tests for interaction with L{twisted.web.guard}.
"""
def setUp(self):
self.avatar_content = b"avatar content"
self.child_content = b"child content"
self.grandchild_content = b"grandchild content"
grandchild = Data(self.grandchild_content, b"text/plain")
child = Data(self.child_content, b"text/plain")
child.putChild(b"grandchild", grandchild)
self.avatar = Data(self.avatar_content, b"text/plain")
self.avatar.putChild(b"child", child)
self.realm = OneIResourceAvatarRealm(self.avatar)
self.portal = Portal(
self.realm,
[AllowAnonymousAccess()],
)
self.guard = HTTPAuthSessionWrapper(
self.portal,
[BasicCredentialFactory("example.com")],
)
def test_avatar(self):
"""
A request for exactly the guarded resource results in the avatar returned
by cred.
"""
root = Page()
root.child_guarded = self.guard
actual = self.successResultOf(
renderResourceReturnTransport(
root,
b"/guarded",
b"GET",
),
)
self.assertIn(self.avatar_content, actual)
def test_child(self):
"""
A request for a direct child of the guarded resource results in that child
of the avatar returned by cred.
"""
root = Page()
root.child_guarded = self.guard
actual = self.successResultOf(
renderResourceReturnTransport(
root,
b"/guarded/child",
b"GET",
),
)
self.assertIn(self.child_content, actual)
def test_grandchild(self):
"""
A request for a grandchild of the guarded resource results in that
grandchild of the avatar returned by cred.
Ideally this test would be redundant with L{test_child} but the
implementation of L{IResource} support results in different codepaths
for the 1st descendant vs the Nth descendant.
"""
root = Page()
root.child_guarded = self.guard
actual = self.successResultOf(
renderResourceReturnTransport(
root,
b"/guarded/child/grandchild",
b"GET",
),
)
self.assertIn(self.grandchild_content, actual)
示例10: makeResource
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
def makeResource(expected):
resource = Data(expected, b"text/plain")
intermediate = Data(b"incorrect intermediate", b"text/plain")
intermediate.putChild(b"child", resource)
return DeferredResource(succeed(intermediate))
示例11: HTTPAuthHeaderTests
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
class HTTPAuthHeaderTests(unittest.TestCase):
"""
Tests for L{HTTPAuthSessionWrapper}.
"""
makeRequest = DummyRequest
def setUp(self):
"""
Create a realm, portal, and L{HTTPAuthSessionWrapper} to use in the tests.
"""
self.username = 'foo bar'
self.password = 'bar baz'
self.avatarContent = "contents of the avatar resource itself"
self.childName = "foo-child"
self.childContent = "contents of the foo child of the avatar"
self.checker = InMemoryUsernamePasswordDatabaseDontUse()
self.checker.addUser(self.username, self.password)
self.avatar = Data(self.avatarContent, 'text/plain')
self.avatar.putChild(
self.childName, Data(self.childContent, 'text/plain'))
self.avatars = {self.username: self.avatar}
self.realm = Realm(self.avatars.get)
self.portal = portal.Portal(self.realm, [self.checker])
self.credentialFactories = []
self.wrapper = HTTPAuthSessionWrapper(
self.portal, self.credentialFactories)
def _authorizedBasicLogin(self, request):
"""
Add an I{basic authorization} header to the given request and then
dispatch it, starting from C{self.wrapper} and returning the resulting
L{IResource}.
"""
authorization = b64encode(self.username + ':' + self.password)
request.headers['authorization'] = 'Basic ' + authorization
return getChildForRequest(self.wrapper, request)
def test_getChildWithDefault(self):
"""
Resource traversal which encounters an L{HTTPAuthSessionWrapper}
results in an L{UnauthorizedResource} instance when the request does
not have the required I{Authorization} headers.
"""
request = self.makeRequest([self.childName])
child = getChildForRequest(self.wrapper, request)
d = request.notifyFinish()
def cbFinished(result):
self.assertEquals(request.responseCode, 401)
d.addCallback(cbFinished)
request.render(child)
return d
def _invalidAuthorizationTest(self, response):
"""
Create a request with the given value as the value of an
I{Authorization} header and perform resource traversal with it,
starting at C{self.wrapper}. Assert that the result is a 401 response
code. Return a L{Deferred} which fires when this is all done.
"""
self.credentialFactories.append(BasicCredentialFactory('example.com'))
request = self.makeRequest([self.childName])
request.headers['authorization'] = response
child = getChildForRequest(self.wrapper, request)
d = request.notifyFinish()
def cbFinished(result):
self.assertEqual(request.responseCode, 401)
d.addCallback(cbFinished)
request.render(child)
return d
def test_getChildWithDefaultUnauthorizedUser(self):
"""
Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
results in an L{UnauthorizedResource} when the request has an
I{Authorization} header with a user which does not exist.
"""
return self._invalidAuthorizationTest('Basic ' + b64encode('foo:bar'))
def test_getChildWithDefaultUnauthorizedPassword(self):
"""
Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
results in an L{UnauthorizedResource} when the request has an
I{Authorization} header with a user which exists and the wrong
password.
"""
return self._invalidAuthorizationTest(
'Basic ' + b64encode(self.username + ':bar'))
def test_getChildWithDefaultUnrecognizedScheme(self):
"""
Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
results in an L{UnauthorizedResource} when the request has an
I{Authorization} header with an unrecognized scheme.
"""
#.........这里部分代码省略.........
示例12: SoledadSessionTestCase
# 需要导入模块: from twisted.web.static import Data [as 别名]
# 或者: from twisted.web.static.Data import putChild [as 别名]
class SoledadSessionTestCase(unittest.TestCase):
"""
Tests adapted from for
L{twisted.web.test.test_httpauth.HTTPAuthSessionWrapper}.
"""
def makeRequest(self, *args, **kwargs):
request = DummyRequest(*args, **kwargs)
request.path = '/'
return request
def setUp(self):
self.username = b'foo bar'
self.password = b'bar baz'
self.avatarContent = b"contents of the avatar resource itself"
self.childName = b"foo-child"
self.childContent = b"contents of the foo child of the avatar"
self.checker = InMemoryUsernamePasswordDatabaseDontUse()
self.checker.addUser(self.username, self.password)
self.avatar = Data(self.avatarContent, 'text/plain')
self.avatar.putChild(
self.childName, Data(self.childContent, 'text/plain'))
self.avatars = {self.username: self.avatar}
self.realm = Realm(self.avatars.get)
self.portal = portal.Portal(self.realm, [self.checker])
self.wrapper = SoledadSession(self.portal)
def _authorizedTokenLogin(self, request):
authorization = b64encode(
self.username + b':' + self.password)
request.requestHeaders.addRawHeader(b'authorization',
b'Token ' + authorization)
return getChildForRequest(self.wrapper, request)
def test_getChildWithDefault(self):
request = self.makeRequest([self.childName])
child = getChildForRequest(self.wrapper, request)
d = request.notifyFinish()
def cbFinished(result):
self.assertEqual(request.responseCode, 401)
d.addCallback(cbFinished)
request.render(child)
return d
def _invalidAuthorizationTest(self, response):
request = self.makeRequest([self.childName])
request.requestHeaders.addRawHeader(b'authorization', response)
child = getChildForRequest(self.wrapper, request)
d = request.notifyFinish()
def cbFinished(result):
self.assertEqual(request.responseCode, 401)
d.addCallback(cbFinished)
request.render(child)
return d
def test_getChildWithDefaultUnauthorizedUser(self):
return self._invalidAuthorizationTest(
b'Basic ' + b64encode(b'foo:bar'))
def test_getChildWithDefaultUnauthorizedPassword(self):
return self._invalidAuthorizationTest(
b'Basic ' + b64encode(self.username + b':bar'))
def test_getChildWithDefaultUnrecognizedScheme(self):
return self._invalidAuthorizationTest(b'Quux foo bar baz')
def test_getChildWithDefaultAuthorized(self):
request = self.makeRequest([self.childName])
child = self._authorizedTokenLogin(request)
d = request.notifyFinish()
def cbFinished(ignored):
self.assertEqual(request.written, [self.childContent])
d.addCallback(cbFinished)
request.render(child)
return d
def test_renderAuthorized(self):
# Request it exactly, not any of its children.
request = self.makeRequest([])
child = self._authorizedTokenLogin(request)
d = request.notifyFinish()
def cbFinished(ignored):
self.assertEqual(request.written, [self.avatarContent])
d.addCallback(cbFinished)
request.render(child)
return d
def test_decodeRaises(self):
request = self.makeRequest([self.childName])
request.requestHeaders.addRawHeader(b'authorization',
b'Token decode should fail')
child = getChildForRequest(self.wrapper, request)
#.........这里部分代码省略.........