• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python的Bottle框架中实现最基本的get和post的方法的教程

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python bottle,bottle框架,美国que bottle京东,que bottle伸缩水壶,baby bottle奶瓶等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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)

</div>

这里注意说一下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: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)

</div>

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

</div>

您可能想查找下面的文章:

  • 关于python的bottle框架跨域请求报错问题的处理方法
  • 使用Python的Bottle框架写一个简单的服务接口的示例
  • 使用Python的Bottle框架写一个简单的服务接口的示例
  • Python的Bottle框架中返回静态文件和JSON对象的方法
  • 简单的连接MySQL与Python的Bottle框架的方法
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • Python的Bottle框架中返回静态文件和JSON对象的方法
  • 简单的连接MySQL与Python的Bottle框架的方法
  • Python的Bottle框架中实现最基本的get和post的方法的教程
  • Python的Bottle框架中获取制定cookie的教程

相关文章

  • 详解Python的Flask框架中的signals信号机制
  • 使用python 获取进程pid号的方法
  • Python的ORM框架中SQLAlchemy库的查询操作的教程
  • python+Django+apache的配置方法详解
  • Python的Tornado框架实现图片上传及图片大小修改功能
  • python不带重复的全排列代码
  • 使用python实现生成用户信息
  • python数据结构之图深度优先和广度优先实例详解
  • Python内置函数的用法实例教程
  • python操作redis的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python利用datetime模块计算时间差
    • 使用Python实现下载网易云音乐的高清MV
    • python处理html转义字符的方法详解
    • Python 字符串操作实现代码(截取/替换/查找/分割)
    • Python验证码识别处理实例
    • python原始套接字编程示例分享
    • 一键搞定python连接mysql驱动有关问题(windows版本)
    • Python yield 使用浅析
    • 10款最好的Web开发的 Python 框架
    • 浅谈Python爬取网页的编码处理

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有