这篇文章主要介绍了Python的Bottle框架中实现最基本的get和post的方法的教程,Bottle框架在Python开发者中的人气很高,需要的朋友可以参考下

1、GET方式:
# -*- coding: utf- -*-
#!/usr/bin/python
# filename: GETPOST_test.py
# codedtime: -- ::
 
 
import bottle
 
def check_login(username, password):
  if username == '' and password == '':
    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-- by the server
##    password = bottle.request.query['password'] # 注:ISO--(即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=)

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

运行效果如下:

2、POST方式:

# -*- coding: utf- -*-
#!/usr/bin/python
# filename: GETPOST_test.py
# codedtime: -- ::
 
 
import bottle
 
def check_login(username, password):
  if username == '' and password == '':
    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=)

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

Python的Bottle框架中实现最基本的get和post的方法的教程的更多相关文章

  1. 关于python的bottle框架跨域请求报错问题的处理

    在用python的bottle框架开发时,前端使用ajax跨域访问时,js代码老是进入不了success,而是进入了error,而返回的状态却是200.url直接在浏览器访问也是正常的,浏览器按F12 ...

  2. Python的Django框架中forms表单类的使用方法详解

    用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...

  3. Python的Django框架中的Cookie相关处理

    Python的Django框架中的Cookie相关处理 浏览器的开发人员在非常早的时候就已经意识到. HTTP's 的无状态会对Web开发人员带来非常大的问题,于是(cookies)应运而生. coo ...

  4. Python的Django框架中的Context使用

    Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...

  5. Python的Django框架中的URL配置与松耦合

    Python的Django框架中的URL配置与松耦合 用 python 处理一个文本时,想要删除其中中某一行,常规的思路是先把文件读入内存,在内存中修改后再写入源文件. 但如果要处理一个很大的文本,比 ...

  6. python之Bottle框架

    一.简单的Bottle框架 1)bottle框架简介 安装 pip install bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架. 此框架只由一个 .py 文件,除 ...

  7. Python之Bottle框架使用

    本文主要包含的内容是Bottle框架介绍和安装使用. 一.Bottle框架介绍 Bottle是一个快速小巧,轻量级的 WSGI 微型 web 框架.同时Bottle也是一个简单高效的遵循WSGI的微型 ...

  8. unittest框架中读取有特殊符号的配置文件内容的方法-configparser的RawConfigParser类应用

    在搭建Unittest框架中,出现了一个问题,配置文件.ini中,出现了特殊字符如何处理? 通过 1.configparser的第三方库对应的ConfigParser类,无法完成对特殊字符的读取: # ...

  9. python nose测试框架中使用allure_report框架

    在使用nose自带的xunit生成xml文件生成测试报告后,领导说报告不够炫,没有百分比效果,且在web自动化时的截图不美观,html很多情况下没有显示图片(nose框架截图方法这里),正好,allu ...

随机推荐

  1. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  2. C++异步编程资料汇集贴

    C++异步编程 http://www.cnblogs.com/zjjcy/archive/2012/03/18/2404214.htmlhttp://www.cnblogs.com/zjjcy/arc ...

  3. mac 上配置 maven

    1. 将maven压缩包解压至/Users/suqiuhui/Applications目录下的新建文件夹dev下 2. 打开终端(系统根目录,~/下) 3. 如果没有 .bash_profile 文件 ...

  4. 面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序

    第一:private, public, protected访问标号的访问范围. private:只能由          1.该类中的函数          2.其友元函数访问 不能被任何其他访问,该 ...

  5. supervisor运行virtualenv环境下的nagios-api

    supervisord-example.conf [unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [su ...

  6. OpenSUSE 内核编译教程 (kernel 2.6.x)

    http://cn.opensuse.org/OpenSUSE_%E5%86%85%E6%A0%B8%E7%BC%96%E8%AF%91%E6%95%99%E7%A8%8B_(kernel_2.6.x ...

  7. day009-IO流

    什么叫流?就是数据的流动.以内存为基准,分为输入input和输出output.输入也叫做读取数据,输出也叫写出数据. 分类 按数据的流向分: 输入流.输出流 按数据类型分:    字节流.字符流 1. ...

  8. 学习笔记-java 多线程

    背景说明: 多线程并发与管理,是java基础知识里的重点,本文根据<java核心技术第八版>中的多线程技术的学习,对知识点进行整理:这里只对基础知识点进行简单罗列,以达到对知识点有网状关联 ...

  9. 关于bootstrap-table服务端分页问题

    昨天项目中涉及到了前端表格分页问题.数据一共有1万多条,所以选择了后端分页. 之前用的都是前端分页,第一次使用后端分页.网上也找到了一些例子,最后做出来了. 这里用的是bootstrap-table插 ...

  10. 密钥导出函数(Key derivation function)

    在密码学中,密钥导出函数(KDF)使用伪随机函数从秘密值(eg.主密钥)导出一个或多个密钥.KDF可用于将密钥扩展到更长的密钥或获得所需格式的密钥(eg.将作为Diffie-Hellman密钥交换的结 ...