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


Posted in Python onApril 30, 2015

1、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)

这里注意说一下Bottle编码的问题,只有第三种方式会将我们输入的字符如果是UTF-8重新编码为UTF-8,当你的内容里有中文或其他非英文字符时,这种方式就显的尤为重要。

运行效果如下:

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

2、POST方式:
 

# -*- 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():
  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)

登录网站、提交文章、评论等我们一般都会用POST方式而非GET方式,那么类似于第二种方式的编码就很用用处,能够正确的处理我们在Form中提交的内容。而第一种则可能会出现传说中的乱码问题,谨记!!!

Python 相关文章推荐
Python错误: SyntaxError: Non-ASCII character解决办法
Jun 08 Python
python中abs&amp;map&amp;reduce简介
Feb 20 Python
Django框架多表查询实例分析
Jul 04 Python
python 字典 按key值大小 倒序取值的实例
Jul 06 Python
Python基于mysql实现学生管理系统
Feb 21 Python
Python3简单实现串口通信的方法
Jun 12 Python
Pycharm中import torch报错的快速解决方法
Mar 05 Python
Python flask框架实现浏览器点击自定义跳转页面
Jun 04 Python
利用python中的matplotlib打印混淆矩阵实例
Jun 16 Python
python实现npy格式文件转换为txt文件操作
Jul 01 Python
Django显示可视化图表的实践
May 10 Python
MATLAB 全景图切割及盒图显示的实现步骤
May 14 Python
Python中使用Beautiful Soup库的超详细教程
Apr 30 #Python
Python中正则表达式的详细教程
Apr 30 #Python
详解在Python程序中使用Cookie的教程
Apr 30 #Python
处理Python中的URLError异常的方法
Apr 30 #Python
介绍Python的Urllib库的一些高级用法
Apr 30 #Python
python插入数据到列表的方法
Apr 30 #Python
Python的Urllib库的基本使用教程
Apr 30 #Python
You might like
MayFish PHP的MVC架构的开发框架
2009/08/13 PHP
discuz程序的PHP加密函数原理分析
2011/08/05 PHP
PHP使用range协议实现输出文件断点续传代码实例
2014/07/04 PHP
分享五个PHP7性能优化提升技巧
2015/12/07 PHP
Aster vs KG BO3 第一场2.18
2021/03/10 DOTA
鼠标移到div,浮层显示明细,弹出层与div的上边距左边距重合(示例代码)
2013/12/14 Javascript
js中window.open()的所有参数详细解析
2014/01/09 Javascript
javascript实现表格增删改操作实例详解
2015/05/15 Javascript
JS中frameset框架弹出层实例代码
2016/04/01 Javascript
JavaScript中String对象的方法介绍
2017/01/04 Javascript
详解JS获取HTML DOM元素的8种方法
2017/06/17 Javascript
详解node字体压缩插件font-spider的用法
2018/09/28 Javascript
JavaScript数据结构之栈实例用法
2019/01/18 Javascript
layui自己添加图片按钮并点击跳转页面的例子
2019/09/14 Javascript
简单了解vue 插值表达式Mustache
2020/07/22 Javascript
python dict.get()和dict['key']的区别详解
2016/06/30 Python
Python中的is和==比较两个对象的两种方法
2017/09/06 Python
Python模块的加载讲解
2019/01/15 Python
Django框架会话技术实例分析【Cookie与Session】
2019/05/24 Python
python对矩阵进行转置的2种处理方法
2019/07/17 Python
Django 开发环境配置过程详解
2019/07/18 Python
Django接收照片储存文件的实例代码
2020/03/07 Python
如何基于线程池提升request模块效率
2020/04/18 Python
微软开源最强Python自动化神器Playwright(不用写一行代码)
2021/01/05 Python
html5的新玩法——语音搜索
2013/01/03 HTML / CSS
canvas因为图片资源不在同一域名下而导致的跨域污染画布的解决办法
2019/01/18 HTML / CSS
详解HTML5中CSS外观属性
2020/09/10 HTML / CSS
澳大利亚连衣裙和女装在线:Esther
2017/11/11 全球购物
Vertbaudet西班牙网上商店:婴儿服装、童装、母婴用品和儿童家具
2019/10/16 全球购物
企业口号大全
2014/06/12 职场文书
2014年后勤工作总结
2014/11/18 职场文书
2014年妇女工作总结
2014/12/06 职场文书
大学生学年个人总结
2015/02/15 职场文书
借条如何写
2015/05/26 职场文书
企业财务管理制度范本
2015/08/04 职场文书
高一作文之暖冬
2019/11/09 职场文书