CherryPy sessions for same domain, different port

27 views
Skip to first unread message

Jason

unread,
Apr 5, 2010, 11:19:19 PM4/5/10
to cherrypy-users
I have a problem in CP 3.1 where sessions interfere with each other.
This problem is not present in CP 3.0. I asked about this a while
ago[1] and never resolved it, but upgrading to CP 3.1 was not urgent
so I forgot about it (incidentally, the answer on that thread doesn't
work even after fixing the pickling). I'd like to try and sort this
out though, since there are more compelling reasons to move to CP 3.1
now.

Consider the script at the end of this message. It will launch two
subprocesses, each one a cherrypy app (hit Ctrl+C or whatever the
KeyboardInterrupt combo is on your system to end them both). If you
run it with CP 3.0 (taking care to change the 3.0/3.1 specific lines
in "StartServer"), then visit:

http://localhost:15002/

...you see an empty dict. Then visit:

http://localhost:15002/set?val=10
http://localhost:15002/

...and you see the newly populated dict. Then visit:

http://localhost:15012/

...and go back to

http://localhost:15002/

...and nothing has changed.

If you try the same thing with CP 3.1 (remember the lines in
"StartServer"!), when you get to the last step, the dict is now empty.
This happens in Windows and Debian, Python 2.5 and 2.6.

You can try all sorts of things: changing to file storage, separating
the storage paths... the only difference it makes is that the sessions
might get merged instead of erased. I've read another post[2] about
this as well, and there's a suggestion there to put the session tools
config keys in the app config rather than the global config, but I
don't think that's relevant to this usage where the apps run
independently.

Anyway, I'm a little lost about this but I'd like to understand what's
going on and how to fix it. Any pointers would be appreciated.

Cheers,
Jason

[1] http://groups.google.com/group/cherrypy-users/browse_thread/thread/6d28924c7a8e9725
[2] http://groups.google.com/group/cherrypy-users/browse_thread/thread/bcae0fe34ad4cbcb/53f31a360f9b8cf7

Test script:
========
import os, os.path, socket, sys
import subprocess
import cgi

import cherrypy

HTTP_PORT = 15002
HTTP_HOST = "127.0.0.1"

site1conf = {
'global' : {
'server.socket_host' : HTTP_HOST,
'server.socket_port' : HTTP_PORT,
'tools.sessions.on' : True,
# 'tools.sessions.storage_type': 'file',
# 'tools.sessions.storage_path': '1',
# 'tools.sessions.storage_path': '.',
'tools.sessions.timeout' : 1440}}

site2conf = {
'global' : {
'server.socket_host' : HTTP_HOST,
'server.socket_port' : HTTP_PORT + 10,
'tools.sessions.on' : True,
#0 'tools.sessions.storage_type': 'file',
# 'tools.sessions.storage_path': '2',
# 'tools.sessions.storage_path': '.',
'tools.sessions.timeout' : 1440}}


class Home(object) :

def __init__(self, key):
self.key = key

@cherrypy.expose
def index(self):
return """\
<html>
<body>Session:
<br>%s
</body>
</html> """ % cgi.escape(str(dict(cherrypy.session)))

@cherrypy.expose
def set(self, val):
cherrypy.session[self.key.upper()] = val
return """\
<html>
<body>Set %s to %s</body>
</html>""" % (cgi.escape(self.key), cgi.escape(val))

def StartServer(conf, key):
cherrypy.config.update(conf)

print 'Starting server (%s)' % key
cherrypy.tree.mount(Home(key), '/', {})

# Start the web server.
#### 3.0
# cherrypy.server.quickstart()
# cherrypy.engine.start()
####

#### 3.1
cherrypy.engine.start()
cherrypy.engine.block()
####

def Main():
# Start first webserver
proc1 = subprocess.Popen(
[sys.executable, os.path.abspath(__file__), "1"])
proc2 = subprocess.Popen(
[sys.executable, os.path.abspath(__file__), "2"])

proc1.wait()
proc2.wait()

if __name__ == "__main__":

print sys.argv

if len(sys.argv) == 1:
# Master process
Main()
elif(int(sys.argv[1]) == 1):
StartServer(site1conf, 'magic')
elif(int(sys.argv[1]) == 2):
StartServer(site2conf, 'science')
else:
sys.exit(1)

========

Reply all
Reply to author
Forward
0 new messages