本文整理匯總了Python中twisted.web.server.Site方法的典型用法代碼示例。如果您正苦於以下問題:Python server.Site方法的具體用法?Python server.Site怎麽用?Python server.Site使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.web.server
的用法示例。
在下文中一共展示了server.Site方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def __init__(self, db):
self.prefix = ""
root = Resource()
prefix = root
for path in config.PREFIX.split('/'):
if len(path):
r = Resource()
prefix.putChild(path, r)
prefix = r
self.prefix += "/%s" % path
prefix.putChild("trigger", Triggers(db))
prefix.putChild("tag", Tags(db))
prefix.putChild("pattern", Patterns(db))
prefix.putChild("event", Events(db))
prefix.putChild("contact", Contacts(db))
prefix.putChild("subscription", Subscriptions(db))
prefix.putChild("user", Login(db))
prefix.putChild("notification", Notifications(db))
server.Site.__init__(self, root)
示例2: boot_frontend
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def boot_frontend(config, debug=False):
"""
Boot a Pyramid WSGI application as Twisted component
"""
http_port = int(config.get('config-web', 'http_port'))
websocket_uri = unicode(config.get('wamp', 'listen'))
# https://stackoverflow.com/questions/13122519/serving-pyramid-application-using-twistd/13138610#13138610
config = resource_filename('kotori.frontend', 'development.ini')
application = get_app(config, 'main')
# https://twistedmatrix.com/documents/13.1.0/web/howto/web-in-60/wsgi.html
resource = WSGIResource(reactor, reactor.getThreadPool(), application)
reactor.listenTCP(http_port, Site(resource))
示例3: startService
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def startService(self):
"""
Start TCP listener on designated HTTP port,
serving ``HttpChannelContainer`` as root resource.
"""
# Don't start service twice
if self.running == 1:
return
self.running = 1
# Prepare startup
http_listen = self.settings.kotori.http_listen
http_port = int(self.settings.kotori.http_port)
log.info('Starting HTTP service on {http_listen}:{http_port}', http_listen=http_listen, http_port=http_port)
# Configure root Site object and start listening to requests.
# This must take place only once - can't bind to the same port multiple times!
factory = LocalSite(self.root)
reactor.listenTCP(http_port, factory, interface=http_listen)
示例4: testDistrib
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def testDistrib(self):
# site1 is the publisher
r1 = resource.Resource()
r1.putChild("there", static.Data("root", "text/plain"))
site1 = server.Site(r1)
self.f1 = PBServerFactory(distrib.ResourcePublisher(site1))
self.port1 = reactor.listenTCP(0, self.f1)
self.sub = distrib.ResourceSubscription("127.0.0.1",
self.port1.getHost().port)
r2 = resource.Resource()
r2.putChild("here", self.sub)
f2 = MySite(r2)
self.port2 = reactor.listenTCP(0, f2)
agent = client.Agent(reactor)
d = agent.request(b"GET", "http://127.0.0.1:%d/here/there" % \
(self.port2.getHost().port,))
d.addCallback(client.readBody)
d.addCallback(self.assertEqual, 'root')
return d
示例5: setUp
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def setUp(self):
plainRoot = Data(b'not me', 'text/plain')
tlsRoot = Data(b'me neither', 'text/plain')
plainSite = server.Site(plainRoot, timeout=None)
tlsSite = server.Site(tlsRoot, timeout=None)
self.tlsPort = reactor.listenSSL(
0, tlsSite,
contextFactory=ssl.DefaultOpenSSLContextFactory(
serverPEMPath, serverPEMPath),
interface="127.0.0.1")
self.plainPort = reactor.listenTCP(0, plainSite, interface="127.0.0.1")
self.plainPortno = self.plainPort.getHost().port
self.tlsPortno = self.tlsPort.getHost().port
plainRoot.putChild(b'one', Redirect(self.getHTTPS('two')))
tlsRoot.putChild(b'two', Redirect(self.getHTTP('three')))
plainRoot.putChild(b'three', Redirect(self.getHTTPS('four')))
tlsRoot.putChild(b'four', Data(b'FOUND IT!', 'text/plain'))
示例6: test_getPageDeprecated
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_getPageDeprecated(self):
"""
L{client.getPage} is deprecated.
"""
port = reactor.listenTCP(
0, server.Site(Data(b'', 'text/plain')), interface="127.0.0.1")
portno = port.getHost().port
self.addCleanup(port.stopListening)
url = networkString("http://127.0.0.1:%d" % (portno,))
d = client.getPage(url)
warningInfo = self.flushWarnings([self.test_getPageDeprecated])
self.assertEqual(len(warningInfo), 1)
self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
self.assertEqual(
warningInfo[0]['message'],
"twisted.web.client.getPage was deprecated in "
"Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent instead")
return d.addErrback(lambda _: None)
示例7: test_downloadPageDeprecated
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_downloadPageDeprecated(self):
"""
L{client.downloadPage} is deprecated.
"""
port = reactor.listenTCP(
0, server.Site(Data(b'', 'text/plain')), interface="127.0.0.1")
portno = port.getHost().port
self.addCleanup(port.stopListening)
url = networkString("http://127.0.0.1:%d" % (portno,))
path = FilePath(self.mktemp())
d = client.downloadPage(url, path.path)
warningInfo = self.flushWarnings([self.test_downloadPageDeprecated])
self.assertEqual(len(warningInfo), 1)
self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
self.assertEqual(
warningInfo[0]['message'],
"twisted.web.client.downloadPage was deprecated in "
"Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent instead")
return d.addErrback(lambda _: None)
示例8: test_sessionUIDGeneration
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_sessionUIDGeneration(self):
"""
L{site.getSession} generates L{Session} objects with distinct UIDs from
a secure source of entropy.
"""
site = server.Site(resource.Resource())
# Ensure that we _would_ use the unpredictable random source if the
# test didn't stub it.
self.assertIdentical(site._entropy, os.urandom)
def predictableEntropy(n):
predictableEntropy.x += 1
return (unichr(predictableEntropy.x) * n).encode("charmap")
predictableEntropy.x = 0
self.patch(site, "_entropy", predictableEntropy)
a = self.getAutoExpiringSession(site)
b = self.getAutoExpiringSession(site)
self.assertEqual(a.uid, b"01" * 0x20)
self.assertEqual(b.uid, b"02" * 0x20)
# This functionality is silly (the value is no longer used in session
# generation), but 'counter' was a public attribute since time
# immemorial so we should make sure if anyone was using it to get site
# metrics or something it keeps working.
self.assertEqual(site.counter, 2)
示例9: test_processingFailedNoTraceback
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_processingFailedNoTraceback(self):
"""
L{Request.processingFailed} when the site has C{displayTracebacks} set
to C{False} does not write out the failure, but give a generic error
message.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.site.displayTracebacks = False
fail = failure.Failure(Exception("Oh no!"))
request.processingFailed(fail)
self.assertNotIn(b"Oh no!", request.transport.written.getvalue())
self.assertIn(
b"Processing Failed", request.transport.written.getvalue()
)
# Since we didn't "handle" the exception, flush it to prevent a test
# failure
self.assertEqual(1, len(self.flushLoggedErrors()))
示例10: test_processingFailedDisplayTraceback
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_processingFailedDisplayTraceback(self):
"""
L{Request.processingFailed} when the site has C{displayTracebacks} set
to C{True} writes out the failure.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.site.displayTracebacks = True
fail = failure.Failure(Exception("Oh no!"))
request.processingFailed(fail)
self.assertIn(b"Oh no!", request.transport.written.getvalue())
# Since we didn't "handle" the exception, flush it to prevent a test
# failure
self.assertEqual(1, len(self.flushLoggedErrors()))
示例11: test_processingFailedDisplayTracebackHandlesUnicode
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_processingFailedDisplayTracebackHandlesUnicode(self):
"""
L{Request.processingFailed} when the site has C{displayTracebacks} set
to C{True} writes out the failure, making UTF-8 items into HTML
entities.
"""
d = DummyChannel()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.site.displayTracebacks = True
fail = failure.Failure(Exception(u"\u2603"))
request.processingFailed(fail)
self.assertIn(b"☃", request.transport.written.getvalue())
# On some platforms, we get a UnicodeError when trying to
# display the Failure with twisted.python.log because
# the default encoding cannot display u"\u2603". Windows for example
# uses a default encodig of cp437 which does not support u"\u2603".
self.flushLoggedErrors(UnicodeError)
# Since we didn't "handle" the exception, flush it to prevent a test
# failure
self.assertEqual(1, len(self.flushLoggedErrors()))
示例12: test_sessionDifferentFromSecureSession
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_sessionDifferentFromSecureSession(self):
"""
L{Request.session} and L{Request.secure_session} should be two separate
sessions with unique ids and different cookies.
"""
d = DummyChannel()
d.transport = DummyChannel.SSL()
request = server.Request(d, 1)
request.site = server.Site(resource.Resource())
request.sitepath = []
secureSession = request.getSession()
self.assertIsNotNone(secureSession)
self.addCleanup(secureSession.expire)
self.assertEqual(request.cookies[0].split(b"=")[0],
b"TWISTED_SECURE_SESSION")
session = request.getSession(forceNotSecure=True)
self.assertIsNotNone(session)
self.assertEqual(request.cookies[1].split(b"=")[0], b"TWISTED_SESSION")
self.addCleanup(session.expire)
self.assertNotEqual(session.uid, secureSession.uid)
示例13: test_sessionAttribute
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_sessionAttribute(self):
"""
On a L{Request}, the C{session} attribute retrieves the associated
L{Session} only if it has been initialized. If the request is secure,
it retrieves the secure session.
"""
site = server.Site(resource.Resource())
d = DummyChannel()
d.transport = DummyChannel.SSL()
request = server.Request(d, 1)
request.site = site
request.sitepath = []
self.assertIs(request.session, None)
insecureSession = request.getSession(forceNotSecure=True)
self.addCleanup(insecureSession.expire)
self.assertIs(request.session, None)
secureSession = request.getSession()
self.addCleanup(secureSession.expire)
self.assertIsNot(secureSession, None)
self.assertIsNot(secureSession, insecureSession)
self.assertIs(request.session, secureSession)
示例14: test_retrieveNonExistentSession
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def test_retrieveNonExistentSession(self):
"""
L{Request.getSession} generates a new session if the relevant cookie is
set in the incoming request.
"""
site = server.Site(resource.Resource())
d = DummyChannel()
request = server.Request(d, 1)
request.site = site
request.sitepath = []
request.received_cookies[b'TWISTED_SESSION'] = b"does-not-exist"
session = request.getSession()
self.assertIsNotNone(session)
self.addCleanup(session.expire)
self.assertTrue(request.cookies[0].startswith(b'TWISTED_SESSION='))
# It should be a new session ID.
self.assertNotIn(b"does-not-exist", request.cookies[0])
示例15: webserver_for_test
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Site [as 別名]
def webserver_for_test(test, url_path, response_content):
"""
Create a webserver that serves ``response_content`` from ``url_path``.
"""
app = Klein()
@app.route(url_path)
def _respond(request):
return response_content
factory = Site(app.resource())
endpoint = serverFromString(reactor, b"tcp:0")
listening = endpoint.listen(factory)
def stop_port(port):
test.addCleanup(port.stopListening)
return port
listening.addCallback(stop_port)
return listening