spyce
home
license
community
download
examples
resources
wishlist
contrib (@sf)
documentation
intro
lang
runtime
modules
tags
install
exits
sourceforge
statistics
freshmeat

Examples
[[ Spyce ]]
Python Server Pages
by Rimon Barr

examples/mysession.spy
[[.import names="pool,session"]]
[[-- note: this eg will not work under CGI
     or when you have multiple servers --]]
[[\
  # storing session info as server pool variable,
  # so that example is portable! -- you'll 
  # want to have some persistent DB connection.
  if not pool.has_key('mysession'):
    pool['mysession'] = {}
    pool['mysessioncount'] = 1
  def myget(info, id):
    import time
    try:
      state, expiretime = pool['mysession'][id]
      if int(time.time()) > expiretime:
        del pool['mysession'][id]
        return None
      return state
    except KeyError:
      return None
  def myset(info, state, expire, serverID, id):
    import time
    if not id:
      # lock here if running threaded engine
      id = str(pool['mysessioncount'])
      pool['mysessioncount'] = pool['mysessioncount'] + 1
    pool['mysession'][id] = state, int(time.time())+expire
    return id
  def mydel(info, id):
    try: del pool['mysession'][id]
    except KeyError: pass
  def myids(info):
    return pool['mysession'].keys()
  session.setHandler('session_user', myget, myset, mydel, myids, 'mysession')
  session.autoSession(10)
]]
<html><body>
  [[-- count visits --]]
  [[\ 
    if not session.auto: session.auto = 1
    else: session.auto = session.auto + 1
  ]]
  [[-- output --]]
  You have visited this page [[=session.auto]] time(s)<br>
  Your autosession ID is: [[=session.autoID]]<br>
  Autosession expiration = 10 seconds.<br>
  <b>Note:</b> This example requires a persistent server (i.e. non-CGI)
  to function correctly.
</body></html>
Run this code.
(requires Spyce-enabled web server)

Back to List of Examples


© 2002-06 Rimon Barr
email: rimon@acm.org
Spyce Powered SourceForge Logo [[ Spyce ]]
Python Server Pages
version 1.3.13