/ module'web'上的<class'attributeerror'=“”>没有属性'config_session'

时间:2019-05-30 08:17:41

标签: python

我正在LPTHW上关注ex25,试图通过 python3 bin / app.py 运行网站http://0.0.0.0:8080/ 目录结构如下:

__init__.py docs        map.py      templates   web
bin     gothonweb   sessions    tests

./bin:
__init__.py __pycache__ app.py

./bin/__pycache__:
__init__.cpython-37.pyc app.cpython-37.pyc

./docs:

./gothonweb:
__init__.py __init__.pyc    __pycache__ map.py      map.pyc

./gothonweb/__pycache__:
__init__.cpython-37.pyc map.cpython-37.pyc

./sessions:
07cb99819409e9eafd8ec3d4bc86cdbb3ac20238

./templates:
layout.html show_room.html  you_died.html

./tests:
__init__.py __pycache__ app_tests.py    map_tests.py    tools.py

./tests/__pycache__:
__init__.cpython-37.pyc     map_tests.cpython-37.pyc
app_tests.cpython-37.pyc    tools.cpython-37.pyc

./web:
__init__.py contrib     http.py     session.py  webapi.py
__pycache__ db.py       httpserver.py   template.py webopenid.py
application.py  debugerror.py   net.py      test.py     wsgi.py
browser.py  form.py     py3helpers.py   utils.py

./web/__pycache__:
__init__.cpython-37.pyc     net.cpython-37.pyc



./web/contrib:
__init__.py __pycache__ template.py

./web/contrib/__pycache__:
__init__.cpython-37.pyc

无法弄清楚如何解决此错误并成功运行网页。

代码文件如下:

app.py

from gothonweb import map

urls=(
    '/game','GameEngine',
    '/','Index',
)

app=web.application(urls,globals())

#title hack so that debug mode works with sessions
if web.config.get('_session')is None:
    store=web.session.DiskStore('sessions')
    session=web.session.Session(app,store,initializer={'room':None})
    web.config._session=session
else:
    session=web.config_session

render=web.template.render('templates/',base="layout")

class Index(object):
    def GET(self):
        #this is used to "setup" the session with starting values
        session.room=map.START 
        web.seeother("/game")

class GameEngine(object):
    def GET(self):
        if session.room:
            return render.show_room(room=session.room)
        else:
            #Why is there here?do you need it?
            return render.you_died()
    def POST(self):
        form=web.input(action=None)
        #there is a bug here,can you fix it?
        if session.room and form.action:
            session.room=session.room.go(form.action)
        else:
            session.room=None


if __name__=="__main__":
    app.run()

tools.py

import re

def assert_response(resp,contains=None,matches=None,headers=None,status="200"):
    assert status in resp.status,"Excepted response %r not in %r" %(status,resp.status)
    if status=="200":
        assert resp.data,"Response data is empty."
    if contains:
        assert contains in resp.data,"Response dose not contain %r"%contains
    if matches:
        reg=re.compile(matches)
        assert reg.matches(resp.data),"Response does not match %r"%matches
    if headers:
        assert_equal(resp.headers,headers)

app_tests.py

from bin.app import app
from tests.tools import assert_response

def test_index():
    #check that we get a 303 on the /URL
    #this is because web.seeother()will always send the browser this http code
    resp=app.request("/")
    assert_response(resp,status="303")
def test_game():
    #checkt that we get a 200 on /game
    resp=app.request("/game")
    assert_response(resp,status='200')

    #check that we have response data on a form submit
    resp=app.request ("/game",method="POST")
    assert_response(resp)

打开网址后,页面上显示以下结果

module 'web' has no attribute 'config_session'```
and traceback
AttributeError: module 'web' has no attribute 'config_session'

1 个答案:

答案 0 :(得分:0)

您在app.py顶部的else拼写错误。具体来说,您的else语句尝试访问web.config_session而不是web.config._session