本文整理匯總了Python中twisted.web.server.Session方法的典型用法代碼示例。如果您正苦於以下問題:Python server.Session方法的具體用法?Python server.Session怎麽用?Python server.Session使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.web.server
的用法示例。
在下文中一共展示了server.Session方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_sessionUIDGeneration
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [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)
示例2: test_sessionAttribute
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [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)
示例3: test_startCheckingExpiration
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_startCheckingExpiration(self):
"""
L{server.Session.startCheckingExpiration} causes the session to expire
after L{server.Session.sessionTimeout} seconds without activity.
"""
self.session.startCheckingExpiration()
# Advance to almost the timeout - nothing should happen.
self.clock.advance(self.session.sessionTimeout - 1)
self.assertIn(self.uid, self.site.sessions)
# Advance to the timeout, the session should expire.
self.clock.advance(1)
self.assertNotIn(self.uid, self.site.sessions)
# There should be no calls left over, either.
self.assertFalse(self.clock.calls)
示例4: test_touch
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_touch(self):
"""
L{server.Session.touch} updates L{server.Session.lastModified} and
delays session timeout.
"""
# Make sure it works before startCheckingExpiration
self.clock.advance(3)
self.session.touch()
self.assertEqual(self.session.lastModified, 3)
# And after startCheckingExpiration
self.session.startCheckingExpiration()
self.clock.advance(self.session.sessionTimeout - 1)
self.session.touch()
self.clock.advance(self.session.sessionTimeout - 1)
self.assertIn(self.uid, self.site.sessions)
# It should have advanced it by just sessionTimeout, no more.
self.clock.advance(1)
self.assertNotIn(self.uid, self.site.sessions)
示例5: test_checkExpiredDeprecated
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_checkExpiredDeprecated(self):
"""
L{server.Session.checkExpired} is deprecated.
"""
self.session.checkExpired()
warnings = self.flushWarnings([self.test_checkExpiredDeprecated])
self.assertEqual(warnings[0]['category'], DeprecationWarning)
self.assertEqual(
warnings[0]['message'],
"Session.checkExpired is deprecated since Twisted 9.0; sessions "
"check themselves now, you don't need to.")
self.assertEqual(len(warnings), 1)
# Conditional requests:
# If-None-Match, If-Modified-Since
# make conditional request:
# normal response if condition succeeds
# if condition fails:
# response code
# no body
示例6: getAutoExpiringSession
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def getAutoExpiringSession(self, site):
"""
Create a new session which auto expires at cleanup.
@param site: The site on which the session is created.
@type site: L{server.Site}
@return: A newly created session.
@rtype: L{server.Session}
"""
session = site.makeSession()
# Clean delayed calls from session expiration.
self.addCleanup(session.expire)
return session
示例7: test_makeSession
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_makeSession(self):
"""
L{site.getSession} generates a new C{Session} instance with an uid of
type L{bytes}.
"""
site = server.Site(resource.Resource())
session = self.getAutoExpiringSession(site)
self.assertIsInstance(session, server.Session)
self.assertIsInstance(session.uid, bytes)
示例8: setUp
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def setUp(self):
"""
Create a site with one active session using a deterministic, easily
controlled clock.
"""
self.clock = Clock()
self.uid = b'unique'
self.site = server.Site(resource.Resource())
self.session = server.Session(self.site, self.uid, self.clock)
self.site.sessions[self.uid] = self.session
示例9: test_defaultReactor
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_defaultReactor(self):
"""
If not value is passed to L{server.Session.__init__}, the global
reactor is used.
"""
session = server.Session(server.Site(resource.Resource()), b'123')
self.assertIdentical(session._reactor, reactor)
示例10: test_expire
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_expire(self):
"""
L{server.Session.expire} expires the session.
"""
self.session.expire()
# It should be gone from the session dictionary.
self.assertNotIn(self.uid, self.site.sessions)
# And there should be no pending delayed calls.
self.assertFalse(self.clock.calls)
示例11: test_expireWhileChecking
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_expireWhileChecking(self):
"""
L{server.Session.expire} expires the session even if the timeout call
isn't due yet.
"""
self.session.startCheckingExpiration()
self.test_expire()
示例12: test_notifyOnExpire
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_notifyOnExpire(self):
"""
A function registered with L{server.Session.notifyOnExpire} is called
when the session expires.
"""
callbackRan = [False]
def expired():
callbackRan[0] = True
self.session.notifyOnExpire(expired)
self.session.expire()
self.assertTrue(callbackRan[0])
示例13: test_touch
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def test_touch(self):
"""
L{server.Session.touch} updates L{server.Session.lastModified} and
delays session timeout.
"""
# Make sure it works before startCheckingExpiration
self.clock.advance(3)
self.session.touch()
self.assertEqual(self.session.lastModified, 3)
# And after startCheckingExpiration
self.session.startCheckingExpiration()
self.clock.advance(self.session.sessionTimeout - 1)
self.session.touch()
self.clock.advance(self.session.sessionTimeout - 1)
self.assertIn(self.uid, self.site.sessions)
# It should have advanced it by just sessionTimeout, no more.
self.clock.advance(1)
self.assertNotIn(self.uid, self.site.sessions)
# Conditional requests:
# If-None-Match, If-Modified-Since
# make conditional request:
# normal response if condition succeeds
# if condition fails:
# response code
# no body
示例14: __init__
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def __init__(self, postpath, session=None):
self.sitepath = []
self.written = []
self.finished = 0
self.postpath = postpath
self.prepath = []
self.session = None
self.protoSession = session or Session(0, self)
self.args = {}
self.requestHeaders = Headers()
self.responseHeaders = Headers()
self.responseCode = None
self._finishedDeferreds = []
self._serverName = b"dummy"
self.clientproto = b"HTTP/1.0"
示例15: getSession
# 需要導入模塊: from twisted.web import server [as 別名]
# 或者: from twisted.web.server import Session [as 別名]
def getSession(self):
if self.session:
return self.session
assert not self.written, "Session cannot be requested after data has been written."
self.session = self.protoSession
return self.session