2012-11-10 28 views
1

我不熟悉CherryPy和Python,但我需要編寫一個非常簡單的web應用程序,它執行登錄--->執行一些命令--->註銷。對於登錄我使用的代碼下面的鏈接:如何從簡單的Web應用程序註銷。在CherryPy中,Python

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> 

與文件夾包含三個圖片。

當我運行應用程序時,我可以看到本地主機上的用戶名和密碼字段的登錄頁面,然後我可以到達有三個按鈕「ON,OFF,註銷」的網頁。

問題是我必須點擊退出按鈕兩次才能註銷,當我再次登錄並點擊任何按鈕時,即使是ON或OFF按鈕,頁面也會退出並再次顯示登錄頁面。 我無法以正確的方式註銷,有什麼幫助嗎?

謝謝

回答

0

試着運行這段代碼。它調用AuthController().logout()函數。

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) 

希望這會有所幫助。

安德魯

+0

感謝安德魯的答覆,但是當我點擊退出按鈕,我得到了以下內容: – Linux

+0

謝謝安德魯,但是當我點擊退出按鈕我有以下幾點:500內部服務器錯誤 服務器遇到一個意想不到的情況妨礙了它完成請求。 回溯(最近通話最後一個): 文件 「/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py」,線606,在響應 cherrypy.response.body = self.handler() 文件「/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py​​」,第25行,在__call__中 return self.callable(* self.args,** self.kwargs) 文件「run.py」,行33,在索引 auth.logout() NameError:未定義全局名稱'auth' – Linux

+0

我編輯了我的回覆。嘗試AuthController().logout()。希望這可以幫助。 –

相關問題