如何从简单的Web应用程序注销.在CherryPy中,Python
我不熟悉CherryPy和Python,但是我需要编写一个非常简单的Web应用程序来执行登录--->执行一些命令--->注销.对于登录,我使用以下链接中的代码:
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
该应用程序是:
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)
,而html文件为:
<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.
当我运行该应用程序时,我可以在本地主机上看到带有用户名和密码字段的登录页面,然后可以访问具有三个按钮"ON,OFF,Logout"的网页.
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 ?
谢谢
尝试运行此代码.它会调用AuthController().logout()函数.
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)
希望这会有所帮助.
安德鲁