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

yipeiwu_com5年前Python基础

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,当你的内容里有中文或其他非英文字符时,这种方式就显的尤为重要。

运行效果如下:

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)

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

相关文章

nohup后台启动Python脚本,log不刷新的解决方法

问题: =》nohup python3 xxxx.py &后台启动脚本 tail -100f nohup.out    -------->  &nbs...

CentOS 7下安装Python 3.5并与Python2.7兼容并存详解

CentOS 7下安装Python 3.5并与Python2.7兼容并存详解

本文主要给大家介绍了关于在CentOS 7下安装Python 3.5并与Python2.7兼容并存的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: CentOS 7下安装P...

Python生成密码库功能示例

本文实例讲述了Python生成密码库功能。分享给大家供大家参考,具体如下: 这个代码是将字符的所有组合添加到一个文件中,可以设置密码的最大长度,我这里设置的是8位,但是要有心里准备,生成...

python基础教程之自定义函数介绍

函数最重要的目的是方便我们重复使用相同的一段程序。 将一些操作隶属于一个函数,以后你想实现相同的操作的时候,只用调用函数名就可以,而不需要重复敲所有的语句。 函数的定义 首先,我们要定义...

Python基础之函数基本用法与进阶详解

Python基础之函数基本用法与进阶详解

本文实例讲述了Python基础之函数基本用法与进阶。分享给大家供大家参考,具体如下: 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01....