国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python的Bottle框架中實現最基本的get和post的方法的教程

2020-02-23 01:03:05
字體:
來源:轉載
供稿:網友

1、GET方式:
  

# -*- coding: utf-8 -*-#!/usr/bin/python# filename: GETPOST_test.py# codedtime: 2014-9-20 19:07:04import bottledef 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)

這里注意說一下Bottle編碼的問題,只有第三種方式會將我們輸入的字符如果是UTF-8重新編碼為UTF-8,當你的內容里有中文或其他非英文字符時,這種方式就顯的尤為重要。

運行效果如下:

2015430172604482.png (699×104)

2、POST方式:
 

# -*- coding: utf-8 -*-#!/usr/bin/python# filename: GETPOST_test.py# codedtime: 2014-9-20 19:07:04import bottledef check_login(username, password):  if username == '123' and password == '234':    return True  else:    return False@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>      '''@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 "<p> Your login information was correct.</p>"  else:    return "<p>Login failed. </p>"bottle.run(host='localhost', port=8083)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 托里县| 霍山县| 营山县| 雅江县| 积石山| 东辽县| 吉木萨尔县| 抚宁县| 玛多县| 台东县| 界首市| 江都市| 平泉县| 酉阳| 刚察县| 镇沅| 溧阳市| 安图县| 辽阳市| 蓝山县| 前郭尔| 安化县| 江阴市| 阿坝| 永宁县| 彰化县| 呼伦贝尔市| 柘城县| 红河县| 确山县| 广宗县| 桑植县| 新蔡县| 旅游| 广河县| 绥化市| 府谷县| 东台市| 长顺县| 礼泉县| 汉阴县|