Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to share session in the child application? #141

Open
HackerLZH opened this issue Aug 3, 2020 · 11 comments
Open

How to share session in the child application? #141

HackerLZH opened this issue Aug 3, 2020 · 11 comments

Comments

@HackerLZH
Copy link

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?

@cclauss
Copy link
Contributor

cclauss commented Aug 3, 2020

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

@HackerLZH
Copy link
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.

@cclauss
Copy link
Contributor

cclauss commented Aug 3, 2020

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."

@HackerLZH
Copy link
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

@cclauss
Copy link
Contributor

cclauss commented Aug 3, 2020

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

@HackerLZH
Copy link
Author

HackerLZH commented Aug 3, 2020

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!

@cclauss
Copy link
Contributor

cclauss commented Aug 3, 2020

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

@HackerLZH
Copy link
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?

@cclauss
Copy link
Contributor

cclauss commented Aug 3, 2020

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

@HackerLZH
Copy link
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!

@HackerLZH
Copy link
Author

I use python38, not python2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants