使用python瓶发布用户名和密码

时间:2015-07-23 03:52:40

标签: python bottle

我正在学习瓶子,我在他们的网站上看到了登录示例。它看起来像这样:

@route('/login')
def login():
    return '''
        <form action="/login" method="post">
            username: <input name="username" type="text" />
            password: <input name="password" type="password" />
            <input value="Login" type="submit" />
        </form>
            '''

@route('/login', method='POST') 
def do_login():
    username = request.forms.post('username')
    password = request.forms.post('password')
    if check_login(username, password):
        return "<p>You have logged in successfuly</p>"
    else:
        return "<p>Your log in attempt has failed</p>"

我在查看代码时所期望的是能够输入用户名和密码,并查看成功消息或失败消息。但是我一直收到Internal server error消息。

我也尝试过这样的事情:

if username=='username' and password=='password'

我知道它不漂亮,但此时我不确定这一切是如何运作的。这会产生同样的错误。

1 个答案:

答案 0 :(得分:1)

我离开了它,然后在工作而不是在家里重新尝试,并使用以前的版本。现在澄清代码如下:

@route('/login')
def login():
    return '''
        <form action="/login" method="post">
            username: <input name="username" type="text" />
            password: <input name="password" type="password" />
            <input value="Login" type="submit" />
        </form>
           '''

@route('/login', method='POST') 
def do_login():
    username = request.forms.get('username')
    password = request.forms.get('password')
    if username==username and password==password
        return "<p>You have logged in successfuly</p>"
    else:
        return "<p>Your log in attempt has failed</p>"

request.forms.post更改为request.forms.get

之后我做了我自己的check_login版本:

 usernames = ["username", "user"]
 passwords = ["password", "pass"]
 def check_login(username, password):
     if username in usernames and password in passwords:
         return True
     else:
         return False

并将验证更改为:

    if check_login(username, password) is True:
        return "<p>You have logged in succesfully</p>"
    else:
        return "<p>Logging in attempt failed</p>"