python之bottle框架的get,post请求实例

阅读更多
=======================get请求

# coding=utf-8
'''
Created on 2017年5月9日

@author: chenkai
'''
import bottle

def check_login(username, password):
    if username == 'kaige' and password == '123456':
        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
       
        if check_login(username, password):
            return "

登录成功

"
        else:
            return "

登陆失败,用户名或者密码错误

"
    else:
        return '''

                     Username:
                     Password:
                    
                  

                '''

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

运行这个py程序后,浏览器输入:http://localhost:8083/login , 输入用户名和密码,点击登录







===================================post请求

# coding=utf-8
'''
Created on 2017年5月9日

@author: chenkai
'''
import bottle

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

@bottle.route('/login')
def login():
    return '''

                 Username:
                 Password:
                
              

            '''

@bottle.route('/login', method='POST')
def do_login():
    # 第一种方式
#   username = request.forms.get('username')
#   password = request.forms.get('password')

    #第二种方式
    postValue = bottle.request.POST.decode('utf-8')
    username = bottle.request.POST.get('username')
    password = bottle.request.POST.get('password')

   
    if check_login(username, password):
        return "

登录成功

"
    else:
        return "

登录失败

"

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


运行这个py程序后,浏览器输入:http://localhost:8083/login , 输入用户名和密码,点击登录,这个明显是post请求, 而且浏览器不会显示参数


你可能感兴趣的:(python)