1. 程式人生 > >Bottle例項之—POST、GET再解

Bottle例項之—POST、GET再解

# -*- coding:  utf-8 -*-
#!/usr/bin/python
# filename: GETPOST_test.py
# codedtime: 2014-9-20 19:07:04


import bottle

def check_login(username, password):
    if username == '123' and password == '234':
        return True
    else:
        return False

@bottle.route('/login')
def login():
    if bottle.request.GET.get
('do_submit','').strip(): #點選登入按鈕
        # 第一種方式(latin1編碼)
##        username = bottle.request.GET.get('username','').strip()  # 使用者名稱
##        password = bottle.request.GET.get('password','').strip()  # 密碼

        #第二種方式(獲取username\password)(latin1編碼)
        getValue = bottle.request.query_string
##        username = bottle.request.query['username'] # An utf8 string provisionally decoded as ISO-8859-1 by the server
##        password = bottle.request.query['password'] # 注:ISO-8859-1(即aka latin1編碼)
        #第三種方式(獲取UTF-8編碼)
        username = bottle.request.query.username      # The same string correctly re-encoded as utf8 by bottle
        password = bottle.request.query.password      # The same string correctly re-encoded as utf8 by bottle
        
        print('getValue=  '+getValue,
              '\r\nusername=  '
+username,
              '\r\npassword=  '+password) # test
        
        if check_login(username, password):
            return "<p> Your login information was correct.</p>"
        else:
            return "<p>Login failed. </p>"
    else:
        return ''' <form action="/login" method="get">
                     Username: <input name="username" type="text" />
                     Password: <input name="password" type="password" />
                     <input value="Login" name="do_submit" type="submit">
                   </form>
                '''

bottle.run(host='localhost', port=8083)