我的 Beaker、WSGI、Apache2、Python 有什么问题吗?

发布于 2024-11-04 11:29:50 字数 1948 浏览 6 评论 0原文

好的,我的代码如下。我正在使用 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',
)

当我访问 localhost/file.wsgi 等网页时,我将设置 session['value'] 并且它将被打印,正如您从我返回的内容中看到的那样。但是,假设我设置了 session['value'] 并决定注释掉程序的以下部分,

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

保存它,然后转到 localhost/file.wsgi。我期望变量 s =session['value'] 获得我之前设置的值。当我更改并保存文件后大约 5-15 秒后转到 localhost/file.wsgi 时,会话中不再有“值”键,显然它不再有值。显然,访问时间也会发生变化,但创建时间也会发生变化(每次刷新后都会发生变化)。

注意:我还尝试替换程序的一部分,即上面注释掉的部分,

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

在我更改并访问 localhost/file.wsgi 后发生的情况是,会话 ['value'] 会增加(预期),但随后超出它不会跳转并在 0 处创建一个新的会话['value']并再次递增,基本上这会创建多个会话['value'],但会话令牌'mysession'将保持不变。此外,每隔几次刷新该值就会跳回另一个会话['value']并停留在那里,然后跳转到另一个会话['value']。

我对发生的事情感到非常困惑,为什么我设置的会话['value']没有保持设置状态,为什么创建时间不断变化,为什么有多个创建,为什么会话会跳转?

顺便说一句,这在 Chrome 和 Firefox 上都是一样的。

我使用的是 ubuntu 10.10、python 2.6,并且我假设是最新或最新版本的 beaker、apache2、mod_wsgi。

是我编程的问题吗,apache2配置错误,没有正确使用beaker,什么?

非常感谢你们。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文