Python的Bottle框架中实现最基本的get和post的方法的教程
2024-09-03 14:16:07
这篇文章主要介绍了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中提交的内容。而第一种则可能会出现传说中的乱码问题,谨记!!
最新文章
- Qt线程(4) 降低线程占用CPU
- uva10870 矩阵
- 进度的Block在子线程调用
- Javascript动态调整文章的行距、字体、颜色,及打印页面和关闭窗口功能
- find和xargs
- HZAU 17:LCS
- OAuth2.0详解
- java中HashMap的用法
- HDU4612+Tarjan缩点+BFS求树的直径
- 放开那个UI 妹子,让我来(上)
- Laravel5中使用阿里大于(鱼)发送短信验证码
- IDEA 下新建 Hibernate 项目
- 光刻技术的原理和EUV光刻技术前景
- js 把 json 转为以 ‘&;’ 连接的字符串
- 类的命名空间&;组合
- 转载------------C函数之memcpy()函数用法
- 笔记二:常用的h5语义化标签
- Coredata 单表简单使用
- ios开发之--valueForKeyPath的用法
- Oracle 10g使用amdu抽取数据文件
热门文章
- 【Linux】chmod命令
- selendroid之inspector
- 2018年哔哩哔哩bilibili前端开发工程师在线笔试1
- 牛客网数据库SQL实战1-查找最晚入职员工的所有信息
- ABAP的权限检查跟踪(Authorization trace)工具
- 设计模式——建造者模式(BuilderPattern)
- 每天一个linux命令(22):tar命令
- 动态规划(DP),类似LIS,FatMouse&#39;s Speed
- netbackup 8.1安装前注意事项
- ZOJ 1610 Count the Colors【题意+线段树区间更新&;&;单点查询】