#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    How to logout from a simple web appl. in CherryPy, Python


    I am not familiar with CherryPy and Python, but I need to write a very simple web application that performs login ---> do some commands ---> logout. For login I am using the code of Arnar Birgissonin, unfortunatly I am new user here so I cannot put a link her, but the cod title is :
    "Simple authentication and access restrictions helpers"
    AuthenticationAndAccessRestrictions
    on CherryPy website.

    the application is:

    import cherrypy
    import os.path
    import struct
    from auth import AuthController, require, member_of, name_is

    class Server(object):
    led_power=0
    led_switch=1 #Initial LED on

    _cp_config = {
    'tools.sessions.on': True,
    'tools.auth.on': True
    }
    auth = AuthController()
    @cherrypy.expose
    @require()
    def index(self, switch='', power=''):
    if switch:
    self.led_switch = int(switch)
    if power:
    self.led_power = int(power)

    html = open('led.html','r').read()

    if self.led_switch:
    print "ON"
    else:
    print "OFF"

    if self.led_power:
    print "Logout"
    cherrypy.session.clear()

    return html
    index.exposed = True

    configuration
    conf = {
    'global' : {
    'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP
    'server.socket_port': 8080 #server port
    },

    '/images': { #images served as static files
    'tools.staticdir.on': True,
    'tools.staticdir.dir': os.path.abspath('images')
    },

    '/favicon.ico': { #favorite icon
    'tools.staticfile.on': True,
    'tools.staticfile.filename': os.path.abspath("images/bulb.ico")
    }
    }
    cherrypy.quickstart(Server(), config=conf)


    and the html file is:
    <html>
    <head>
    </head>
    <body>
    <br>
    <a href="?switch=1"><img src="images/on.png"></a>
    <a href="?switch=0"><img src="images/off.png"></a>
    <p>
    <a href="?power=1"><img src="images/Logout.png"></a>
    </body>
    </html>


    with a folder contain three images.

    When I run the application I can see the login page on the localhost with username and password fields, then I can reach to the web page which has three button "ON, OFF, Logout".

    The problem is I must click the logout button twice to logout, and when I login again and click on any button even the ON or OFF buttons the page is logout and show me the login page again. I cannot logout in a right way, any help please ?

    Thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    I just got a help from Andrew on stackoverflow website. By changing
    cherrypy.session.clear() to AuthController().logout('/?power=0') and every think work fine.

IMN logo majestic logo threadwatch logo seochat tools logo