Python的Bottle框架中实现最基本的get和post的方法的教程
作者:JohnnyHu90 时间:2022-07-13 13:37:24
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,当你的内容里有中文或其他非英文字符时,这种方式就显的尤为重要。
运行效果如下:
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解析xml字符串的函数
2008-06-10 12:37:00
python selenium爬取斗鱼所有直播房间信息过程详解
2023-11-19 07:00:11
PHP控制反转(IOC)和依赖注入(DI)
2024-05-11 10:09:31
详解Vue2.0里过滤器容易踩到的坑
2024-05-09 10:40:46
基于Python实现图片九宫格切图程序
2023-11-01 21:16:38
![](https://img.aspxhome.com/file/2023/4/92004_0s.png)
Python3实现自定义比较排序/运算符
2023-04-15 23:32:25
Node.js学习入门
2024-05-13 09:58:37
![](https://img.aspxhome.com/file/2023/2/125852_0s.png)
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
2023-10-16 08:05:00
![](https://img.aspxhome.com/file/2023/2/110022_0s.gif)
彻底解决Python包下载慢问题
2021-07-17 16:02:27
![](https://img.aspxhome.com/file/2023/3/115093_0s.png)
JavaScript封装弹框插件的方法
2024-04-30 10:20:32
![](https://img.aspxhome.com/file/2023/3/130283_0s.gif)
Django REST framework视图的用法
2021-02-10 02:51:49
![](https://img.aspxhome.com/file/2023/9/127649_0s.png)
python nohup 实现远程运行不宕机操作
2023-10-21 02:21:44
![](https://img.aspxhome.com/file/2023/8/71438_0s.jpg)
Python列表元素删除和remove()方法详解
2023-04-07 08:48:54
![](https://img.aspxhome.com/file/2023/3/127143_0s.png)
Python3中常见配置文件写法汇总
2022-01-21 11:14:07
![](https://img.aspxhome.com/file/2023/0/110040_0s.png)
python开发飞机大战游戏
2022-10-08 05:15:37
![](https://img.aspxhome.com/file/2023/9/121739_0s.jpg)
Go各时间字符串使用解析
2023-06-25 02:15:17
深入浅出ES6之let和const命令
2024-05-22 10:37:21
python实现健康码查验系统
2022-06-27 06:31:34
![](https://img.aspxhome.com/file/2023/3/97613_0s.jpg)
Oracle时间日期操作方法小结第1/2页
2010-11-29 19:40:00
纯CSS去除按钮以及链接点击时虚线
2009-05-21 10:40:00