Skip to content

How to share session in the child application? #141

Open

Description

@HackerLZH

The version of official cookbook is still 0.3 but I use the latest 0.61.When I learn how to share session in the child application, the solution given by official cookbook can't work. For example, after I used "session = web.ctx.session" in the child application, it reported an error as "AttributeError: 'ThreadedDict' object has no attribute 'session'" .It just did't share the session at all.
What should I do?

Activity

  1. cclauss commented on Aug 3, 2020

    @cclauss
    Contributor

    Parent: web.ctx.session = session
    Child: session = dict(web.ctx).get('session')

  2. HackerLZH commented on Aug 3, 2020

    @HackerLZH
    Author

    Oh, I have written "session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})" in my parent application. After I modified like you and used 'session.login' in the child application, it reported an error as "AttributeError: 'NoneType' object has no attribute 'login'". So it just means the session I get is None.

  3. cclauss commented on Aug 3, 2020

    @cclauss
    Contributor

    Let's use assertions to see where the problem lies...

    Parent:

    web.cxt.session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})
    assert web.cxt.session, "Login failed!"

    Child:

    session = dict(web.ctx).get('session')
    assert session, "Invalid session provided."
  4. HackerLZH commented on Aug 3, 2020

    @HackerLZH
    Author

    Let's use assertions to see where the problem lies...
    Parent:
    web.cxt.session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})
    assert web.cxt.session, "Login failed!"
    Child:
    session = dict(web.ctx).get('session')
    assert session, "Invalid session provided."

    Traceback (most recent call last):
    File "D:/Python/web.py/main.py", line 3, in
    import enter
    File "D:\Python\web.py\enter.py", line 23, in
    assert session, "Invalid session provided."
    AssertionError: Invalid session provided.

    Ok, Invaid session provided

  5. cclauss commented on Aug 3, 2020

    @cclauss
    Contributor

    Are we sure that the parent lines have already been run?

  6. HackerLZH commented on Aug 3, 2020

    @HackerLZH
    Author

    Are we sure that the parent lines have already been run?

    # parent.py
    
    import web
    import child
    web.config.debug = False
    
    urls = (
        '/child', child.app_child,
    )
    
    app = web.application(urls, globals())
    db = web.database(
        dbn='mysql',
        host='127.0.0.1',
        port=3306,
        user='root',
        pw='password',
        db='webpy',
    )
    
    store = web.session.DBStore(db, 'sessions')
    session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})
    web.ctx.session = session
    
    
    if __name__ == '__main__':
        app.run()
    # child.py
    
    import web
    web.config.debug = False
    
    urls = (
        '/hello', 'hello'
    )
    
    app_child = web.application(urls, globals())
    
    session = dict(web.ctx).get('session')
    
    class hello():
        def GET(self):
            return session.login

    Something like that!

  7. cclauss commented on Aug 3, 2020

    @cclauss
    Contributor

    Nope. (The previous post was reformatted with ```python and ``` for readability.)

    The problem is that the line import child is going to run all code at global scope in client.py. This will mean that the client.py line session = dict(web.ctx).get('session') will get run before the parent.py line web.ctx.session = session

  8. HackerLZH commented on Aug 3, 2020

    @HackerLZH
    Author

    Nope. (The previous post was reformatted with python and for readability.)
    The problem is that the line import child is going to run all code at global scope in client.py. This will mean that the client.py line session = dict(web.ctx).get('session') will get run before the parent.py line web.ctx.session = session

    So that's it, how can I run after the parent.py?

  9. cclauss commented on Aug 3, 2020

    @cclauss
    Contributor

    Many ways to do it but perhaps the simplest is...

    import web
    web.config.debug = False
    
    urls = (
        '/hello', 'hello'
    )
    
    app_child = web.application(urls, globals())
    
    class hello():
        def GET(self):
            return web.ctx.session.login
  10. HackerLZH commented on Aug 3, 2020

    @HackerLZH
    Author

    Many ways to do it but perhaps the simplest is...
    import web
    web.config.debug = False

    urls = (
    '/hello', 'hello'
    )

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

    class hello():
    def GET(self):
    return web.ctx.session.login

    AttributeError: 'ThreadedDict' object has no attribute 'session'

    No way, I think I just return to initial time! The simplest way you gave is the same as official cookbook.But it's version 0.3,and I use version 0.61.And I have tried the official cookbook twelve hours ago. I need the latest usage!

  11. HackerLZH commented on Aug 4, 2020

    @HackerLZH
    Author

    I use python38, not python2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions