本文整理汇总了Python中twisted.python.filepath.FilePath.asTextMode方法的典型用法代码示例。如果您正苦于以下问题:Python FilePath.asTextMode方法的具体用法?Python FilePath.asTextMode怎么用?Python FilePath.asTextMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.filepath.FilePath
的用法示例。
在下文中一共展示了FilePath.asTextMode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WebTests
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import asTextMode [as 别名]
class WebTests(TestCase):
# XXX: Treq isn't ported yet, so just tie it with the WSGI tests for now
skip = WSGI_TESTS
def setUp(self):
self.cbdir = FilePath(self.mktemp())
self.cbdir.createDirectory()
config_extras = DottableDict({"node": "testnode",
"worker": "worker1",
"cbdir": self.cbdir.path})
self.config = ComponentConfig("realm1", extra=config_extras)
def test_root_not_required(self):
"""
Not including a '/' path will mean that path has a 404, but children
will still be routed correctly.
"""
temp_reactor = SelectReactor()
r = router.RouterWorkerSession(config=self.config,
reactor=temp_reactor)
# Open the transport
transport = FakeWAMPTransport(r)
r.onOpen(transport)
realm_config = {
u"name": u"realm1",
u'roles': []
}
# Make a file
self.cbdir.child('file.txt').setContent(b"hello!")
r.start_router_realm("realm1", realm_config)
r.start_router_transport(
"component1",
{
u"type": u"web",
u"endpoint": {
u"type": u"tcp",
u"port": 8080
},
u"paths": {
u"static": {
"directory": self.cbdir.asTextMode().path,
"type": u"static"
}
}
})
# Make a request to the WSGI app.
d1 = treq.get("http://localhost:8080/", reactor=temp_reactor)
d1.addCallback(lambda resp: self.assertEqual(resp.code, 404))
d2 = treq.get("http://localhost:8080/static/file.txt",
reactor=temp_reactor)
d2.addCallback(treq.content)
d2.addCallback(self.assertEqual, b"hello!")
def done(results):
for item in results:
if not item[0]:
raise item[1]
d = defer.DeferredList([d1, d2])
d.addCallback(done)
d.addCallback(lambda _: temp_reactor.stop())
def escape():
if temp_reactor.running:
temp_reactor.stop()
temp_reactor.callLater(1, escape)
temp_reactor.run()