CherryPy sessions for same domain, different port
Posted
by detly
on Stack Overflow
See other posts from Stack Overflow
or by detly
Published on 2010-04-08T01:35:12Z
Indexed on
2010/04/08
1:43 UTC
Read the original article
Hit count: 593
Consider the script below. 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:
...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:
...and go back to
...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 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.
What do I do to get independent CherryPy applications to NOT interfere with each other?
Note: I originally asked this on the CherryPy mailing list but haven't had a response yet so I'm trying here. I hope that's okay.
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,
# '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)
© Stack Overflow or respective owner