Python的Bottle框架中实现最基本的get和post的方法的教程
这篇文章主要介绍了Python的Bottle框架中实现最基本的get和post的方法的教程,Bottle框架在Python开发者中的人气很高,需要的朋友可以参考下
# -*- 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的方法的教程的更多相关文章
- 关于python的bottle框架跨域请求报错问题的处理
在用python的bottle框架开发时,前端使用ajax跨域访问时,js代码老是进入不了success,而是进入了error,而返回的状态却是200.url直接在浏览器访问也是正常的,浏览器按F12 ...
- Python的Django框架中forms表单类的使用方法详解
用户表单是Web端的一项基本功能,大而全的Django框架中自然带有现成的基础form对象,本文就Python的Django框架中forms表单类的使用方法详解. Form表单的功能 自动生成HTML ...
- Python的Django框架中的Cookie相关处理
Python的Django框架中的Cookie相关处理 浏览器的开发人员在非常早的时候就已经意识到. HTTP's 的无状态会对Web开发人员带来非常大的问题,于是(cookies)应运而生. coo ...
- Python的Django框架中的Context使用
Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...
- Python的Django框架中的URL配置与松耦合
Python的Django框架中的URL配置与松耦合 用 python 处理一个文本时,想要删除其中中某一行,常规的思路是先把文件读入内存,在内存中修改后再写入源文件. 但如果要处理一个很大的文本,比 ...
- python之Bottle框架
一.简单的Bottle框架 1)bottle框架简介 安装 pip install bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架. 此框架只由一个 .py 文件,除 ...
- Python之Bottle框架使用
本文主要包含的内容是Bottle框架介绍和安装使用. 一.Bottle框架介绍 Bottle是一个快速小巧,轻量级的 WSGI 微型 web 框架.同时Bottle也是一个简单高效的遵循WSGI的微型 ...
- unittest框架中读取有特殊符号的配置文件内容的方法-configparser的RawConfigParser类应用
在搭建Unittest框架中,出现了一个问题,配置文件.ini中,出现了特殊字符如何处理? 通过 1.configparser的第三方库对应的ConfigParser类,无法完成对特殊字符的读取: # ...
- python nose测试框架中使用allure_report框架
在使用nose自带的xunit生成xml文件生成测试报告后,领导说报告不够炫,没有百分比效果,且在web自动化时的截图不美观,html很多情况下没有显示图片(nose框架截图方法这里),正好,allu ...
随机推荐
- IT小小鸟读书笔记2
Part4: 一. 大学的时光真的很容易荒废,自己的实力到头来和自己的成绩单一样空虚,其实自己也是深有同感的. 二. 这个观点我十分的认同:在某个方面比别人多5%的深度,可能拿到的报酬就是 ...
- 11GR2 双节点RAC 配置单节点DG
只记录主要步骤,供大家参考: RAC 搭建单节点 DG 1 修改源数据库开启归档和force loggingalter system set shared_servers=0; alter datab ...
- March 26 2017 Week 13 Sunday
Deliver not your words by number but by weight. 言不在多,而在有物. Do more than talk, say something. I still ...
- 遍历目录树 - Unicode 模式
=info 遍历目录树 支持 Unicode Code by 523066680@163.com 2017-03 V0.5 使用Win32API判断目录硬链接 ...
- LA 3415 保守的老师
题目链接:https://vjudge.net/contest/161820#problem/E 题意: 有一些同学,要从中选出一些同学来,人数尽量多,但是,两两之间要满足至少一个条件(身高差> ...
- 生理周期,POJ(1006)
题目链接:http://poj.org/problem?id=1006 解题报告: 1.枚举天数的时候可以根据前面的结果直接跳过一些错误的答案. ///三个周期是23,28,33, #include ...
- 【[SDOI2013]随机数生成器】
题目 来画柿子吧 我们要求的是 \[f(x)\equiv t(mod\ \ p)\] 其中\(f(1)=x_0,f(x)=af(x-1)+b\) 我们来写几项柿子看看 \[f(1)=x_0\] \[f ...
- 2018.11.18 Sturts2配置详解&常量配置进阶
1.基于struts.xml 的节点参数配置 package节点 action节点 result节点 include节点 2.struts常量配置以及如何修改为自己的想要的配置 2.1struts默认 ...
- 对接融云即时通讯组件SDK,轻松实现App聊天室
我好像特别喜欢做聊天室类的东东,刚折腾完微软的SignalR又折腾App.本来想研究研究XMPP的,由于服务器的搭建问题,先采用一个第三方的吧,看看效果如何.听到弟弟说他们公司用到了融云,我也下载个S ...
- 【遥感专题系列】微波遥感(二、合成孔径雷达SAR基础)
目前使用最广的成像雷达系统就是合成孔径雷达(Synthetic Aperture Radar:SAR),SAR几乎成为了雷达的代名词.本文从应用角度介绍SAR系统的基本知识. 本文主要包括: SAR基 ...