0

Does a Session object maintain the same TCP connection with a client? In the code below, a request from the client is submitted to a handler, the handler creates a sessions object why does session["count"] on an object give a dictionary? A response is then given back to the client, upon another request is the code re-executed? So that another session object is created? How does the session store the previous count information if it did not return a cookie to the client?

from appengine_utilities import sessions

class SubmitHandler(webapp.RequestHandler):
  def get(self):

    session = sessions.Session()
    if "count" in session:
        session["count"]=session["count"]+1
    else:
        session["count"]=1

    template_values={'message':"You have clicked:"+str(session["count"])}
    # render the page using the template engine
    path = os.path.join(os.path.dirname(__file__),'index.html')
    self.response.out.write(template.render(path,template_values))
bereal
  • 32,519
  • 6
  • 58
  • 104
Streamline Astra
  • 307
  • 2
  • 11

1 Answers1

0

You made several questions so let's go one by one:

  • Sessions are not related to TCP connections. A TCP connection is maintained when both client and server agreed upon that using the HTTP Header keep-alive. (Quoted from Pablo Santa Cruz in this answer).
  • Looking at the module session.py in line 1010 under __getitem__ definition I've found the following TODO: It's broke here, but I'm not sure why, it's returning a model object. Could be something along these lines, I haven't debug it myself.
  • From appengine_utilities documentation sessions are stored in Datastore and Memcache or kept entirely as cookies. The first option also involves sending a token to the client to identify it in subsequent requests. Choosing one or another depends on your actual settings or the default ones if you haven't configured your own. Default settings are defined to use the Datastore option.
  • About code re-execution you could check that yourself adding some logging code to count how many times is the function executed.

Something important, I have noticed that this library had it's latest update on 2nd of January 2016, so it has gone unmaintained for 4 years. It would be best if you change to an up to date library, for example the webapp2 session module. Furthermore, Python 2 is sunsetting by this year (1st January 2020) so you might consider switching to python 3 instead.

PD: I found the exact code you posted under this website. In case you took it from there consider next time to include a reference/citation to it's origin.

Happy-Monad
  • 1,962
  • 1
  • 6
  • 13