[Twisted-Python] Patch: Passive getSession for twisted.web.server.Request

Tom 'Korpios' Tobin korpios at korpios.com
Fri May 16 08:38:50 MDT 2003


This patch adds an option to pass a named argument, passive, to twisted.web.server.Request's getSession method.  If passive is true, then getSession will check for the existence of a session or session cookie; it will not, however, drop a session cookie on the client if one does not already exist.  If there is no session, one would expect None from getSession in this case.

This is useful in conditions where one would like to check for (and use) a session if one already exists, but not necessarily drop a session cookie on every client which comes along.


--- server.py   Fri May 16 09:41:15 2003
+++ server-patched.py   Fri May 16 10:32:02 2003
@@ -305,7 +305,7 @@

     session = None

-    def getSession(self, sessionInterface = None):
+    def getSession(self, sessionInterface = None, passive = 0):
         # Session management
         if not self.session:
             cookiename = string.join(['TWISTED_SESSION'] + self.sitepath, "_")
@@ -316,12 +316,13 @@
                 except KeyError:
                     pass
             # if it still hasn't been set, fix it up.
-            if not self.session:
+            if not self.session and not passive:
                 self.session = self.site.makeSession()
                 self.addCookie(cookiename, self.session.uid, path='/')
-        self.session.touch()
-        if sessionInterface:
-            return self.session.getComponent(sessionInterface)
+        if self.session:
+            self.session.touch()
+            if sessionInterface:
+                return self.session.getComponent(sessionInterface)
         return self.session

     def prePathURL(self):


Tom "Korpios" Tobin
korpios at korpios.com





More information about the Twisted-Python mailing list