'Return the existing session or a new session'

def getSession(self):

    if (self.session is not None):
        return self.session
    cookie_str = self.headers.get('Cookie')
    if cookie_str:
        cookie_obj = SimpleCookie(cookie_str)
        sid_morsel = cookie_obj.get(self.SESSION_COOKIE_NAME, None)
        if (sid_morsel is not None):
            sid = sid_morsel.value
        else:
            sid = None
    else:
        sid = None
    if (sid is None):
        sid = randomString(16, '0123456789abcdef')
        session = None
    else:
        session = self.server.sessions.get(sid)
    if (session is None):
        session = self.server.sessions[sid] = {}
    session['id'] = sid
    self.session = session
    return session
