def application(environ, start_response):
    global next_cookie

    # print environ_template % pprint.pformat(environ) # DEBUG, voluminous!

    response_headers = [] # add headers below
    cookie = environ.get('HTTP_COOKIE') # might be None

    # show login page
    if (environ['PATH_INFO'] == '/webapp.py' 
        and environ['REQUEST_METHOD'] == 'GET' 
        and cookie not in sessions): # cookie might be None
        response_body = login_page
        response_headers += [
            ('Set-Cookie','PYSESSID=%s; path=/' % next_cookie)]
        next_cookie += 1
        status = '200 OK'
        
    # log in, if successful show data form page
    elif (environ['PATH_INFO'] == '/webapp.py' 
          and environ['REQUEST_METHOD'] == 'POST'):
        wd = environ['wsgi.input']
        method = environ['REQUEST_METHOD']
        length = int(environ['CONTENT_LENGTH'])
        request_body = wd.read(length)
        vars = urlparse.parse_qs(request_body)
        user = vars['username'][0] # vars[x] are lists, get first item
        passwd = vars['password'][0]
        if user in password and password[user] == passwd:
            sessions[cookie] = user
            if not user in strings:
                strings[user] = ''
            # CORRECT CODE comented out
            # if not user in integers:
            #   integers[user] = 0
            # BUG follows, should be guarded by if ... like strings 
            integers[user] = 0 # BUG, always overwrites data from last session
            # PHP version sends redirect back to doStuff instead of this response_body
            #response_body = dostuff_template % (integers[user], 
            #                                   strings[user])
            response_headers += [('Location','webapp.py')]
            status = "302 Found"
            response_body = ''
        else:
            response_body = login_failure_page
            status = '200 OK'
            
    # submit data in form page
    elif (environ['PATH_INFO'] == '/webapp.py' 
          and environ['REQUEST_METHOD'] == 'GET' 
          and cookie in sessions):
        user = sessions[cookie]
        vars = urlparse.parse_qs(environ['QUERY_STRING'])
        if 'num' in vars:
            integers[user] = str(vars['num'][0]) # vars[x] are lists, 1st item
        if 'str' in vars:
            strings[user] = vars['str'][0]                              
        response_body = dostuff_template % (integers[user], 
                                            strings[user])
        status = '200 OK'

    # log out
    elif environ['PATH_INFO'] == '/logout.py':
        if cookie in sessions:
            del sessions[cookie]
        response_body = '' # blank page, like original NModel version
        status = '200 OK'
        pass
    
    # unknown page
    elif environ['PATH_INFO'] not in ('/webapp.py', '/logout.py'):
        response_body = p404_page        
        status = '404 Not Found'

    # nonsense: doStuff REQUEST_METHOD not GET or POST, or ... ?
    else:
        raise ValueError # send 500 Server Error

    # response
    response_headers += [('Content-Type', 'text/html'),
                         ('Content-Length', str(len(response_body)))]
    start_response(status, response_headers)
    return [response_body]
dataset/ETHPy150Open jon-jacky/PyModel/samples/WebApplication/webapp.py/application/original