Bottle实例之—POST、GET再解
来源:互联网 发布:淘宝重复铺货怎么办 编辑:程序博客网 时间:2024/05/22 15:48
GET、POST的详细区别见: 浅谈HTTP中Get与Post的区别 这里不再赘述,接着 Bottle实例2——Request Routing 的例子我们再来谈谈GET、POST.废话不多说、直接上代码
1、GET方式:
Python Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- 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)
#!/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)
运行效果如下:
2、POST方式:
Python Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- 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)
#!/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)
0 0
- Bottle实例之—POST、GET再解
- Bottle实例之-MySQL连接
- httpClient get post 实例
- Bottle实例1——HelloWord
- Bottle实例2——Request Routing
- 【教程】贺岁教程之--C# get&post实例
- Android实例demo15之HttpUrlConnect的get、post操作、asynchttp
- Ajax之get、post实例(最原始的XMLHttpServlet)
- bottle接收post请求
- Ajax Post 与 Get 实例
- jquery+ajx post/get 实例
- Http get/post 练习实例
- Python的Bottle框架中实现最基本的get和post的方法的教程
- ajax之 $.get() , $.post()
- HttpClient详解(三)—get post封装实例
- bottle 接口实例
- Bottle实例Todo-List—用SQLite3创建数据库
- Bottle实例Todo-List—编辑数据库中的记录
- HAproxy配置实例
- android中activity与service的双向通信
- HDU - 5024 Wang Xifeng's Little Plot
- webkit学习
- UVA - 401 Palindromes
- Bottle实例之—POST、GET再解
- 纪念
- HDU 1846 Brave Game (简单博弈)
- HDU - 5025 Saving Tang Monk
- 今日第一题 uva11384
- Vector 和 ArrayList去重
- cocos2d-X之精灵角色(CCSprite)
- Spring中AOP
- hdu 5023 线段树