Dear All,I am attempting to save sessions in the database and a session id as a cookie on the client. The session id is also used as an identifier on the database. Can someone point me in the right direction on how to do this.It definitely would be of great help.
from werkzeug.contrib.sessions import Session, FilesystemSessionStore
session_store = FilesystemSessionStore()
class SessionMixin(object):
session_key = 'sessionid'
def open_session(self, request):
sid = request.cookies.get(self.session_key, None) or \
request.values.get(self.session_key, None)
if sid is None:
return session_store.new()
else:
return session_store.get(sid)
def save_session(self, session, response):
if session.should_save:
session_store.save(session)
response.set_cookie(self.session_key, session.sid)
return response
class FlaskSess(SessionMixin, Flask): pass
That is a one using file save session like PHP default session. You can
implement SessionStore using database like FilesystemSessionStore in
werkzeug.contrib.sessions.
Another way is using beaker.
Good luck!
2010/10/17 alice ni <alice.ni19@gmail.com>
> Dear All,I am attempting to save sessions in the database and a session id
> as a cookie on the client. The session id is also used as an identifier on
> the database. Can someone point me in the right direction on how to do
> this.It definitely would be of great help.
Thanks heww ,
I wrote a store and tried implementing it. Now all the session details get
stored in the database in a table.Unique session ids are also created ,
which are saved as a cookie on the client. But am unable to unpickle the
data stored in the database.I get a keyError while trying to get the data,it
looks like the get() function of the store has some issue. Request somebody
to please have a look at it.
I have written this for GAE.
The store looks like this:- http://pastebin.com/mSCh4UxH
<http://pastebin.com/mSCh4UxH>The init code looks like this :-
http://pastebin.com/jQMNtXDL
(the code to test is what I took from Armin's badsession code which he had
posted on github)
and the error looks like this:-
File "/home/alice19/alice/flask/app.py", line 821, in request_context
return _RequestContext(self, environ)
File "/home/alice19/alice/flask/ctx.py", line 33, in __init__
self.session = app.open_session(self.request)
File "/home/alice19/travbuddy/alice/__init__.py", line 15, in open_session
return session_store.get(sid)
File "/home/alice19/alice/werkzeug/contrib/sessions.py", line 375, in get
data=loads(rowwithsid.sessiondata)
File "/usr/local/python2.5/lib/python2.5/pickle.py", line 1374, in loads
return Unpickler(file).load()
File "/usr/local/python2.5/lib/python2.5/pickle.py", line 858, in load
dispatch[key](self)
KeyError: '\x00'
<http://pastebin.com/fMeRpBdV>Really need someone'e help here. I know I am
doing something wrong in either the get function or in the class
SessionMixin in init.py
On Sun, Oct 17, 2010 at 5:20 AM, heww0205 <heww0205@gmail.com> wrote:
> from werkzeug.contrib.sessions import Session, FilesystemSessionStore
>
> session_store = FilesystemSessionStore()
>
> class SessionMixin(object):
> session_key = 'sessionid'
>
> def open_session(self, request):
> sid = request.cookies.get(self.session_key, None) or \
> request.values.get(self.session_key, None)
> if sid is None:
> return session_store.new()
> else:
> return session_store.get(sid)
>
> def save_session(self, session, response):
> if session.should_save:
> session_store.save(session)
> response.set_cookie(self.session_key, session.sid)
> return response
>
>
> class FlaskSess(SessionMixin, Flask): pass
>
>
> That is a one using file save session like PHP default session. You can
> implement SessionStore using database like FilesystemSessionStore in
> werkzeug.contrib.sessions.
>
> Another way is using beaker.
>
> Good luck!
>
>
> 2010/10/17 alice ni <alice.ni19@gmail.com>
>
> Dear All,I am attempting to save sessions in the database and a session id
>> as a cookie on the client. The session id is also used as an identifier on
>> the database. Can someone point me in the right direction on how to do
>> this.It definitely would be of great help.
>
>
>
I solved that basically the column in the table in which the data was being pickled and stored had to be BlobProperty. I was using TextProperty. But I am sure their would be many more issues. As of now it seems to work fine. Will check again! On Sun, Oct 17, 2010 at 9:55 AM, alice ni <alice.ni19@gmail.com> wrote: > Thanks heww , > I wrote a store and tried implementing it. Now all the session details get > stored in the database in a table.Unique session ids are also created , > which are saved as a cookie on the client. But am unable to unpickle the > data stored in the database.I get a keyError while trying to get the data,it > looks like the get() function of the store has some issue. Request somebody > to please have a look at it. > I have written this for GAE. > > The store looks like this:- http://pastebin.com/mSCh4UxH > <http://pastebin.com/mSCh4UxH>The init code looks like this :- > http://pastebin.com/jQMNtXDL > (the code to test is what I took from Armin's badsession code which he had > posted on github) > and the error looks like this:- > > File "/home/alice19/alice/flask/app.py", line 821, in request_context > return _RequestContext(self, environ) > File "/home/alice19/alice/flask/ctx.py", line 33, in __init__ > self.session = app.open_session(self.request) > File "/home/alice19/travbuddy/alice/__init__.py", line 15, in open_session > return session_store.get(sid) > File "/home/alice19/alice/werkzeug/contrib/sessions.py", line 375, in get > data=loads(rowwithsid.sessiondata) > File "/usr/local/python2.5/lib/python2.5/pickle.py", line 1374, in loads > return Unpickler(file).load() > File "/usr/local/python2.5/lib/python2.5/pickle.py", line 858, in load > dispatch[key](self) > KeyError: '\x00' > > <http://pastebin.com/fMeRpBdV>Really need someone'e help here. I know I am > doing something wrong in either the get function or in the class > SessionMixin in init.py > > > On Sun, Oct 17, 2010 at 5:20 AM, heww0205 <heww0205@gmail.com> wrote: > >> from werkzeug.contrib.sessions import Session, FilesystemSessionStore >> >> session_store = FilesystemSessionStore() >> >> class SessionMixin(object): >> session_key = 'sessionid' >> >> def open_session(self, request): >> sid = request.cookies.get(self.session_key, None) or \ >> request.values.get(self.session_key, None) >> if sid is None: >> return session_store.new() >> else: >> return session_store.get(sid) >> >> def save_session(self, session, response): >> if session.should_save: >> session_store.save(session) >> response.set_cookie(self.session_key, session.sid) >> return response >> >> >> class FlaskSess(SessionMixin, Flask): pass >> >> >> That is a one using file save session like PHP default session. You can >> implement SessionStore using database like FilesystemSessionStore in >> werkzeug.contrib.sessions. >> >> Another way is using beaker. >> >> Good luck! >> >> >> 2010/10/17 alice ni <alice.ni19@gmail.com> >> >> Dear All,I am attempting to save sessions in the database and a session id >>> as a cookie on the client. The session id is also used as an identifier on >>> the database. Can someone point me in the right direction on how to do >>> this.It definitely would be of great help. >> >> >> >
It works perfectly now...,.well for me at least...It would be great if somebody else looking for the same thing could test it too and let me know all those amateurish things I just might have done.:-) The corrected code is here http://pastebin.com/t6V5JLjF and Yes, Flask Rocks!Thanks to all of you! On Sun, Oct 17, 2010 at 11:26 AM, alice ni <alice.ni19@gmail.com> wrote: > I solved that basically the column in the table in which the data was being > pickled and stored had to be BlobProperty. I was using TextProperty. But I > am sure their would be many more issues. As of now it seems to work fine. > Will check again! > > > On Sun, Oct 17, 2010 at 9:55 AM, alice ni <alice.ni19@gmail.com> wrote: > >> Thanks heww , >> I wrote a store and tried implementing it. Now all the session details >> get stored in the database in a table.Unique session ids are also created , >> which are saved as a cookie on the client. But am unable to unpickle the >> data stored in the database.I get a keyError while trying to get the data,it >> looks like the get() function of the store has some issue. Request somebody >> to please have a look at it. >> I have written this for GAE. >> >> The store looks like this:- http://pastebin.com/mSCh4UxH >> <http://pastebin.com/mSCh4UxH>The init code looks like this :- >> http://pastebin.com/jQMNtXDL >> (the code to test is what I took from Armin's badsession code which he had >> posted on github) >> and the error looks like this:- >> >> File "/home/alice19/alice/flask/app.py", line 821, in request_context >> return _RequestContext(self, environ) >> File "/home/alice19/alice/flask/ctx.py", line 33, in __init__ >> self.session = app.open_session(self.request) >> File "/home/alice19/travbuddy/alice/__init__.py", line 15, in open_session >> return session_store.get(sid) >> File "/home/alice19/alice/werkzeug/contrib/sessions.py", line 375, in get >> data=loads(rowwithsid.sessiondata) >> File "/usr/local/python2.5/lib/python2.5/pickle.py", line 1374, in loads >> return Unpickler(file).load() >> File "/usr/local/python2.5/lib/python2.5/pickle.py", line 858, in load >> dispatch[key](self) >> KeyError: '\x00' >> >> <http://pastebin.com/fMeRpBdV>Really need someone'e help here. I know I >> am doing something wrong in either the get function or in the class >> SessionMixin in init.py >> >> >> On Sun, Oct 17, 2010 at 5:20 AM, heww0205 <heww0205@gmail.com> wrote: >> >>> from werkzeug.contrib.sessions import Session, FilesystemSessionStore >>> >>> session_store = FilesystemSessionStore() >>> >>> class SessionMixin(object): >>> session_key = 'sessionid' >>> >>> def open_session(self, request): >>> sid = request.cookies.get(self.session_key, None) or \ >>> request.values.get(self.session_key, None) >>> if sid is None: >>> return session_store.new() >>> else: >>> return session_store.get(sid) >>> >>> def save_session(self, session, response): >>> if session.should_save: >>> session_store.save(session) >>> response.set_cookie(self.session_key, session.sid) >>> return response >>> >>> >>> class FlaskSess(SessionMixin, Flask): pass >>> >>> >>> That is a one using file save session like PHP default session. You can >>> implement SessionStore using database like FilesystemSessionStore in >>> werkzeug.contrib.sessions. >>> >>> Another way is using beaker. >>> >>> Good luck! >>> >>> >>> 2010/10/17 alice ni <alice.ni19@gmail.com> >>> >>> Dear All,I am attempting to save sessions in the database and a session >>>> id as a cookie on the client. The session id is also used as an identifier >>>> on the database. Can someone point me in the right direction on how to do >>>> this.It definitely would be of great help. >>> >>> >>> >> >