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通过urllib2爬网页上种子下载示例
Feb 24 Python
Python合并两个字典的常用方法与效率比较
Jun 17 Python
Python3使用正则表达式爬取内涵段子示例
Apr 22 Python
Python合并多个Excel数据的方法
Jul 16 Python
Django unittest 设置跳过某些case的方法
Dec 26 Python
详解使用Python下载文件的几种方法
Oct 13 Python
Python综合应用名片管理系统案例详解
Jan 03 Python
使用keras内置的模型进行图片预测实例
Jun 17 Python
Python爬虫实例——爬取美团美食数据
Jul 15 Python
Python求区间正整数内所有素数之和的方法实例
Oct 13 Python
python链表类中获取元素实例方法
Feb 23 Python
Pandas加速代码之避免使用for循环
May 30 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
PHP安装全攻略:APACHE
2006/10/09 PHP
解析php通过cookies获取远程网页的指定代码
2013/06/25 PHP
PHP获取当前页面URL函数实例
2014/10/22 PHP
Laravel模板引擎Blade中section的一些标签的区别介绍
2015/02/10 PHP
Laravel中基于Artisan View扩展包创建及删除应用视图文件的方法
2016/10/08 PHP
在 Laravel 项目中使用 webpack-encore的方法
2019/07/21 PHP
JS cookie中文乱码解决方法
2014/01/28 Javascript
常见的javascript跨域通信方法
2015/12/31 Javascript
常用Javascript函数与原型功能收藏(必看篇)
2016/10/09 Javascript
Jquery循环截取字符串的方法(多出的字符串处理成&quot;...&quot;)
2016/11/28 Javascript
动态加载css方法实现和深入解析
2017/01/18 Javascript
Angularjs分页查询的实现
2017/02/24 Javascript
微信小程序多音频播放进度条问题
2018/08/28 Javascript
JS重学系列之聊聊new操作符
2019/03/04 Javascript
详解jquery和vue对比
2019/04/16 jQuery
[01:06:32]DOTA2上海特级锦标赛D组资格赛#1 EG VS VP第一局
2016/02/28 DOTA
python实现将html表格转换成CSV文件的方法
2015/06/28 Python
Python多线程实现同步的四种方式
2017/05/02 Python
详解python中的文件与目录操作
2017/07/11 Python
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
2019/06/27 Python
Python如何应用cx_Oracle获取oracle中的clob字段问题
2019/08/27 Python
python MD5加密的示例
2020/10/19 Python
python调用百度API实现人脸识别
2020/11/17 Python
GNC健安喜官方海外旗舰店:美国著名保健品牌
2017/01/04 全球购物
巴基斯坦购物网站:Goto
2019/03/11 全球购物
巴西网上药店:Drogaria Araujo
2021/01/06 全球购物
得到Class的三个过程是什么
2012/08/10 面试题
一年级学生评语
2014/04/23 职场文书
个人股份转让协议书范本
2014/10/26 职场文书
2015年党总支工作总结
2015/05/25 职场文书
网络妈妈观后感
2015/06/08 职场文书
基层工作经历证明
2015/06/19 职场文书
2015小学教育教学工作总结
2015/07/21 职场文书
车位出租协议书范本
2016/03/19 职场文书
Android学习之BottomSheetDialog组件的使用
2022/06/21 Java/Android