Python的Bottle框架中实现最基本的get和post的方法的教程
作者:JohnnyHu90 发布时间:2022-07-13 13:37:24
标签: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,当你的内容里有中文或其他非英文字符时,这种方式就显的尤为重要。
运行效果如下:
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这个argparse,虽然基本能理解,但没有仔细自己动手去写,因此这里写下来作为自己本人的
- 一、工具python3第三方类库requestspython3-pyqt5(GUI依赖,不用GUI可不装)ubuntu系列系统使用以下命令安
- 由于js的代码逻辑越来越重,一个js文件可能会有上千行,十分不利于开发与维护。最近正在把逻辑很重的js拆分成模块,在一顿纠结是使用requi
- 增大 SGA 已经缓冲看来对于性能的提升并不显著,加载时间只提升了 1.73%。下面我们增加 SGA 重做日志的大小: DB3: Log B
- 如何把[1, 5, 6, [2, 7, [3, [4, 5, 6]]]]变成[1, 5, 6, 2, 7, 3, 4, 5, 6]?思考:-
- 代码如下#encoding:utf-8import requestsfrom lxml import etreeimport xlwtimp
- //********************** index.asp ************************//<
- 离散特征的编码分为两种情况:1、离散特征的取值之间没有大小的意义,比如color:[red,blue],那么就使用one-hot编码2、离散
- 今天咱写一个挺实用的工具,就是扫描并获取可用的proxy首先呢,我先百度找了一个网站:http://www.xicidaili.com 作为
- 使用在Safari和WebKit中可用的CSS高级特性,你可以为你的网站和网络应用带来一个新的级别的令人兴奋的东西。WebKit是Safar
- 本次爬虫用到的网址是:http://www.netbian.com/index.htm: 彼岸桌面.里面有很多的好看壁纸,而且都是可以下载高
- 语言的内存管理是语言设计的一个重要方面。它是决定语言性能的重要因素。无论是C语言的手工管理,还是Java的垃圾回收,都成为语言最重要的特征。
- tf.reverse_sequence()简述在看bidirectional_dynamic_rnn()的源码的时候,看到了代码中有调用 r
- 先把这个script加到你的页面里:http://code.google.com/p/doufu/source/browse/trunk/n
- 本文实例讲述了Python实现对象转换为xml的方法。分享给大家供大家参考,具体如下:# -*- coding:UTF-8 -*-'
- 大家都知道Vue.js是中国人创造出来的,简单易用,所以必须要支持一下Vue采用的MVVM设计模式也就是说model和view绑定 mode
- 前几天翻出以前写的一个纯CSS仿微软经典菜单,现在看来才感叹,微软的经典菜单确实很经典,至少看起来不觉得厌烦。感叹归感叹,想想既然可以实现下
- 不论什么时候,只要系统带有多个设备,而这些设备的性能又各不相同,就存在从慢速设备到快速设备不断更换工作地点以改善系统性能的可能性,这就是缓存
- 如何使DIV居中,div垂直居中,div水平居中.这个问题在用CSS来设计网页的时候经常会遇到,如果用传统的表格来布局是很简单的,CSS里就
- 不需要依赖第三方组件的vue日期移动端组件 小轮子 轻量可复用: https://github.com/