Meidokon Wiki
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Useful(?) links

  • furinkan's stuff

  • Postfix snippets


  • SystemInfo

  • This sidebar

Navigation

  • FrontPage
  • RecentChanges
  • FindPage
  • HelpContents

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

MeidokonWiki:
  • furinkan
  • python
  • session_parsing

Python http session parsing

Generic session-handling code.

   1 #!/usr/bin/python
   2 
   3 import hashlib, shelve, time, Cookie, os
   4 
   5 class Session(object):
   6 
   7    def __init__(self, expires=None, cookie_path=None):
   8       string_cookie = os.environ.get('HTTP_COOKIE', '')
   9       self.cookie = Cookie.SimpleCookie()
  10       self.cookie.load(string_cookie)
  11 
  12       if self.cookie.get('sid'):
  13          sid = self.cookie['sid'].value
  14          # Clear session cookie from other cookies
  15          self.cookie.clear()
  16 
  17       else:
  18          self.cookie.clear()
  19          sid = hashlib.sha1(repr(time.time())).hexdigest()
  20 
  21       self.cookie['sid'] = sid
  22 
  23       if cookie_path:
  24          self.cookie['sid']['path'] = cookie_path
  25 
  26       session_dir = '/tmp'
  27       if not os.path.exists(session_dir):
  28          try:
  29             os.mkdir(session_dir, 02770)
  30          # If the apache user can't create it create it manualy
  31          except OSError, e:
  32             errmsg =  """%s when trying to create the session directory. Create it as '%s'""" % (e.strerror, os.path.abspath(session_dir))
  33             raise OSError, errmsg
  34       self.data = shelve.open(session_dir + '/sess_' + sid, writeback=True)
  35       os.chmod(session_dir + '/sess_' + sid, 0660)
  36 
  37       # Initializes the expires data
  38       if not self.data.get('cookie'):
  39          self.data['cookie'] = {'expires':''}
  40 
  41       self.set_expires(expires)
  42 
  43    def close(self):
  44       self.data.close()
  45 
  46    def set_expires(self, expires=None):
  47       if expires == '':
  48          self.data['cookie']['expires'] = ''
  49       elif isinstance(expires, int):
  50          self.data['cookie']['expires'] = expires
  51 
  52       self.cookie['sid']['expires'] = self.data['cookie']['expires']

And used for a basic site self-test.

   1 # 2. session handling
   2 import session
   3 try:
   4         sess = session.Session(expires='', cookie_path='/')
   5         sess.data['lastvisit'] = repr("this is a string")
   6         sess.close()
   7 except Exception, data:
   8         page_output.append_and_finish("* FAIL starting the session: %s" % str(data))
   9 page_output.append("* PASS started a session")
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01
MoinMoin Release 1.9.11 [Revision release], Copyright by Juergen Hermann et al.