What's wrong with my Beaker, WSGI, Apache2, Py

2019-05-29 10:52发布

问题:

Okay so my code is below. I'm working with apache2, mod_wsgi, beaker, python

def application(environ, start_response):
    session = environ['beaker.session']
    if not session.has_key('value'):
        session['value'] = 0
    session.save()
    try:
        s = session['value']
    except:
        s = "value not found"
    start_response('200 OK', [('Content-type', 'text/html')])
    #response = "<br />".join(environ)
    beaker = "<br />".join(session)
    try:
        cookie = "".join(environ['HTTP_COOKIE'])
    except:
        cookie = ""
    return [cookie,'<br />', str(s), '<br />', beaker, '<br /> accessed:', 
str(session['_accessed_time']), '<br /> creation:', 
str(session['_creation_time'])]

from beaker.middleware import SessionMiddleware

application = SessionMiddleware(
    application,
    key='mysession',
    secret='blah',
)

When I go to my webpage like localhost/file.wsgi, I will set session['value'] and it will be printed as you can see from what I return. However, say after I set session['value'] and decide to comment out the following part of the program,

    #if not session.has_key('value'):
     #   session['value'] = 0
    #session.save()

save it, and go to localhost/file.wsgi. I was expecting for the variable s, which is =session['value'], to get the value I set previously. When I go to localhost/file.wsgi after maybe 5-15 seconds after I change and save the file, there is no longer a 'value' key in session and obviously it no longer has a value. What also changes is the accessed time, obviously, but the creation time also changes (and does so after every refresh).

Note: I also tried replacing part of the program, the part I commented out above, with

    if not session.has_key('value'):
        session['value'] = 0
    session['value'] += 1
    session.save()

What happened after I changed and accessed localhost/file.wsgi was that the session['value'] would increment(expected), but then out of nowhere it would jump and create a new session['value'] at 0 and increment again, and basically this would create multiple session['value']s, but the session token 'mysession' would stay the same. Also every few refreshes the value would jump back to another session['value'] and stay there and then jump to another session['value'].

I am so confused at what's happening, how come the session['value'] I set doesn't stay set, how come the creation times keep changing, why are there multiple creations, and why did the sessions jump around?

btw this was the same on both chrome and firefox.

I'm on ubuntu 10.10, python 2.6, and I'm assuming the newest or around the newest versions of beaker, apache2, mod_wsgi.

Is it my programming's problem, badly configed apache2, didn't use beaker correctly, what?

Thank you guys so much.