设为首页 加入收藏

TOP

Python的Bottle框架中实现最基本的get和post的方法的教程(一)
2019-08-23 00:42:48 】 浏览:74
Tags:Python Bottle 框架 实现 基本 get post 方法 教程

这篇文章主要介绍了Python的Bottle框架中实现最基本的get和post的方法的教程,Bottle框架在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,当你的内容里有中文或其他非英文字符时,这种方式就显的尤为重要。

运行效果如下:

2015430172604482.png (699×104)

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:
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php接口编程 下一篇[Linux] Nginx 提供静态内容和优..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目