2015-05-06 42 views
4

我正在嘗試創建一個非常基本的cherrypy webapp,它會在加載第一個(也是唯一)頁面之前向用戶詢問用戶名和密碼。我使用的例子在CherryPy的文檔這裏闡述:http://cherrypy.readthedocs.org/en/latest/basics.html#authenticationCherryPy web應用程序的基本身份驗證

這裏是我的具體代碼wsgi.py:

import cherrypy 
from cherrypy.lib import auth_basic 
from myapp import myapp 

USERS = {'jon': 'secret'} 

def validate_password(username, password): 
    if username in USERS and USERS[username] == password: 
     return True 
    return False 

conf = { 
    '/': { 
     'tools.auth_basic.on': True, 
     'tools.auth_basic.realm': 'localhost', 
     'tools.auth_basic.checkpassword': validate_password 
    } 
} 

if __name__ == '__main__': 

    cherrypy.config.update({ 
     'server.socket_host': '127.0.0.1', 
     'server.socket_port': 8080, 
    }) 

    # Run the application using CherryPy's HTTP Web Server 
    cherrypy.quickstart(myapp(), '/', conf) 

上面的代碼會得到我的瀏覽器用戶/當我點擊確定以迅速傳遞但是提示,我得到以下錯誤:

Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 667, in respond 
    self.hooks.run('before_handler') 
    File "/usr/local/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 114, in run 
    raise exc 
TypeError: validate_password() takes exactly 2 arguments (3 given) 

我不知道它認爲它是從哪裏得到第三個參數。有什麼想法嗎?謝謝!

回答

3

從CherryPy的

checkpassword: a callable which checks the authentication credentials. 
     Its signature is checkpassword(realm, username, password). where 
     username and password are the values obtained from the request's 
     'authorization' header. If authentication succeeds, checkpassword 
     returns True, else it returns False. 

的文檔,所以你的checkpassword的實現必須遵循相同的API,它是:checkpassword(realm, username, password)。而你是什麼節目我們缺少第一個參數 - 境界。

+0

請原諒我的無知,但什麼是「境界」? – fender4645

+0

@ user3693009我可以原諒無知,但不是懶惰!首先回答谷歌「什麼是領域基本認證」; http://stackoverflow.com/questions/12701085/what-is-the-realm-in-basic-authentication –

+0

對不起...我現在在代碼中看到它要求領域(謝謝,我做了Google它和現在明白了)。 – fender4645