本文整理汇总了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