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网络编程之TCP通信实例和socketserver框架使用例子
Apr 25 Python
Python编程之多态用法实例详解
May 19 Python
浅谈Python爬取网页的编码处理
Nov 04 Python
matplotlib绘制符合论文要求的图片实例(必看篇)
Jun 02 Python
python+selenium实现登录账户后自动点击的示例
Dec 22 Python
Python3.6笔记之将程序运行结果输出到文件的方法
Apr 22 Python
python3实现小球转动抽奖小游戏
Apr 15 Python
如何将 awk 脚本移植到 Python
Dec 09 Python
keras 指定程序在某块卡上训练实例
Jun 22 Python
python获取系统内存占用信息的实例方法
Jul 17 Python
浅谈Python3中datetime不同时区转换介绍与踩坑
Aug 02 Python
Python OpenCV形态学运算示例详解
Apr 07 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 strtotime函数详解
2009/12/18 PHP
Ping服务的php实现方法,让网站快速被收录
2012/02/04 PHP
ThinkPHP框架设计及扩展详解
2014/11/25 PHP
php+ajax实现无刷新分页
2015/11/18 PHP
Yii隐藏URL中index.php的方法
2016/07/12 PHP
浅谈PHP中的错误处理和异常处理
2017/02/04 PHP
PHP实现将优酷土豆腾讯视频html地址转换成flash swf地址的方法
2017/08/04 PHP
php判断某个方法是否存在函数function_exists (),method_exists()与is_callable()区别与用法解析
2020/04/20 PHP
中文路径导致unitpngfix.js不正常的解决方法
2013/06/26 Javascript
一个js控制的导航菜单实例代码
2013/12/03 Javascript
js window对象属性和方法相关资料整理
2015/11/11 Javascript
浅谈jQuery 选择器和dom操作
2016/06/07 Javascript
KnockoutJS 3.X API 第四章之数据控制流component绑定
2016/10/10 Javascript
Bootstrap3 图片(响应式图片&amp;图片形状)
2017/01/04 Javascript
JavaScript定义全局对象的方法示例
2017/01/12 Javascript
引入JavaScript时alert弹出框显示中文乱码问题
2017/09/16 Javascript
angular json对象push到数组中的方法
2018/02/27 Javascript
Node.js中你不可不精的Stream(流)
2018/06/08 Javascript
Vue项目中配置pug解析支持
2019/05/10 Javascript
iview的table组件自带的过滤器实现
2019/07/12 Javascript
Vue使用NProgress的操作过程解析
2019/10/10 Javascript
详解设计模式中的工厂方法模式在Python程序中的运用
2016/03/02 Python
在Python中实现替换字符串中的子串的示例
2018/10/31 Python
Django模型验证器介绍与源码分析
2020/09/08 Python
python中操作文件的模块的方法总结
2021/02/04 Python
厨师岗位职责
2013/11/12 职场文书
银行优秀员工事迹材料
2014/05/29 职场文书
信息员培训方案
2014/06/12 职场文书
2014年学校领导班子对照检查材料
2014/09/19 职场文书
四风问题查摆剖析材料
2014/10/11 职场文书
学习十八届四中全会依法治国心得体会
2014/11/03 职场文书
公司副总经理岗位职责
2015/04/08 职场文书
2015年司机年终工作总结
2015/05/14 职场文书
《1942》观后感
2015/06/08 职场文书
职工食堂管理制度
2015/08/06 职场文书
什么是检讨书?检讨书的格式及范文
2019/11/05 职场文书