本文整理汇总了Python中twisted.web.static.Data类的典型用法代码示例。如果您正苦于以下问题:Python Data类的具体用法?Python Data怎么用?Python Data使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Data类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
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
def test_getChild(self):
"""
L{NameVirtualHost.getChild} returns correct I{Resource} based off
the header and modifies I{Request} to ensure proper prepath and
postpath are set.
"""
virtualHostResource = NameVirtualHost()
leafResource = Data(b"leaf data", "")
leafResource.isLeaf = True
normResource = Data(b"norm data", "")
virtualHostResource.addHost(b'leaf.example.org', leafResource)
virtualHostResource.addHost(b'norm.example.org', normResource)
request = DummyRequest([])
request.requestHeaders.addRawHeader(b'host', b'norm.example.org')
request.prepath = [b'']
self.assertIsInstance(virtualHostResource.getChild(b'', request),
NoResource)
self.assertEqual(request.prepath, [b''])
self.assertEqual(request.postpath, [])
request = DummyRequest([])
request.requestHeaders.addRawHeader(b'host', b'leaf.example.org')
request.prepath = [b'']
self.assertIsInstance(virtualHostResource.getChild(b'', request),
Data)
self.assertEqual(request.prepath, [])
self.assertEqual(request.postpath, [b''])
示例3: test_resource_tree
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')
示例4: go
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()
示例5: get_provider_factory
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
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: setUp
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)
示例8: setUp
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)
示例9: makeResource
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))
示例10: __init__
def __init__(self, data, type, status=OK):
Data.__init__(self, data, type)
self._status = status
示例11: len
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()
示例12: render_POST
def render_POST(self, request):
request.setResponseCode(self._status)
self.posted += (request.content.read(),)
return Data.render_GET(self, request)
示例13: SoledadSessionTestCase
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)
#.........这里部分代码省略.........
示例14: len
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()
示例15: GuardTests
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)