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

夙愿已清 提交于 2020-01-16 04:02:28

问题


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 in the following link:

http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions

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


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


回答1:


Try running this code. It calls the AuthController().logout() function.

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"
        AuthController().logout()

        return html
    index.exposed = True


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)

Hope this helps.

Andrew



来源:https://stackoverflow.com/questions/13318215/how-to-logout-from-a-simple-web-appl-in-cherrypy-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!