热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:python之Bottle框架

篇首语:本文由编程笔记#小编为大家整理,主要介绍了python之Bottle框架相关的知识,希望对你有一定的参考价值。一、简单的Bottle框架1)bott

篇首语:本文由编程笔记#小编为大家整理,主要介绍了python之Bottle框架相关的知识,希望对你有一定的参考价值。


一、简单的Bottle框架

1)bottle框架简介



安装 pip install bottle
Bottle是一个快速、简洁、轻量级的基于WSIG的微型Web框架。
此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块。


bottle简介

2)bottle框架的组成部分



1、路由系统,将不同请求交由指定函数处理
2、模板系统,将模板中的特殊语法渲染成字符串,值得一说的是Bottle的模板引擎可以任意指定:Bottle内置模板、mako、jinja2、cheetah
3、公共组件,用于提供处理请求相关的信息,如:表单数据、COOKIEs、请求头等
4、服务,Bottle默认支持多种基于WSGI的服务


bottle框架的组成部分

 Bottle默认支持多种基于WSGI的服务



server_names = {
\'cgi\': CGIServer,
\'flup\': FlupFCGIServer,
\'wsgiref\': WSGIRefServer,
\'waitress\': WaitressServer,
\'cherrypy\': CherryPyServer,
\'paste\': PasteServer,
\'fapws3\': FapwsServer,
\'tornado\': TornadoServer,
\'gae\': AppEngineServer,
\'twisted\': TwistedServer,
\'diesel\': DieselServer,
\'meinheld\': MeinheldServer,
\'gunicorn\': GunicornServer,
\'eventlet\': EventletServer,
\'gevent\': GeventServer,
\'geventSocketIO\':GeventSocketIOServer,
\'rocket\': RocketServer,
\'bjoern\' : BjoernServer,
\'auto\': AutoServer,
}


WSGI的服务

3)框架的基本使用



#!/usr/bin/env python
#
-*- coding:utf-8 -*-
from bottle import template,Bottle
root
= Bottle()
@root.route(
\'/hello\')
def index():
#
return "Hello World"
return template(\'Hello {{ name }}!\',name="user")
root.run(host
=\'localhost\',port=8080)


bottle简单使用

访问:  http://localhost:8080/hello

4)对于form表单提前,python后端取值




"en">

"UTF-8">



Bottle登录系统


"/login/" method="POST">
"text" name="user" placeholder="用户名"/>
"password" name="pwd" placeholder="密码"/>
"submit" value="提交"/>




login.html


@root.route(\'/login/\',method=["POST","GET"])
def login():
if request.method == "GET":
return template(\'login.html\')
else:
u
= request.forms.get(\'user\')
p
= request.forms.get(\'pwd\')
return redirect(\'/index/\')


request.forms.get()取值

二 、路由系统

1)静态路由



@root.route(\'/hello/\')
def index():
return template(\'Hello {{name}}!\', name="User")


静态路由

2)动态路由



@root.route(\'/wiki/\')
def callback(pagename):
...

@root.route(
\'/object/\')
def callback(id):
...

@root.route(
\'/show/\')
def callback(name):
...

@root.route(
\'/static/\')
def callback(path):
return static_file(path, root=\'static\')


动态路由

3)请求方法路由



@root.route(\'/hello/\', method=\'POST\')
def index():
...

@root.
get(\'/hello/\')
def index():
...

@root.post(
\'/hello/\')
def index():
...

@root.put(
\'/hello/\')
def index():
...

@root.delete(
\'/hello/\')
def index():
...

# 第一种,写在一起
@root.route(
\'/login/\',method=["POST","GET"])
def login():
if request.method == "GET":
return template(\'login.html\')
else:
return redirect(\'/index/\')

# 第二种,分开写
@root.route(
\'/login/\',method="POST")
def index():
return template(\'login.html\')
@root.route(
\'/login/\',method="GET")
def index():
return template(\'login.html\')


请求方法路由

4)二级路由,路由分发

主路由编辑



#!/usr/bin/env python
#
-*- coding:utf-8 -*-
from bottle import template, Bottle
from bottle import static_file
root
= Bottle()

@root.route(
\'/hello/\')
def index():
return template(\'Root {{name}}!\', name="Alex")

from framwork_bottle import app01
from framwork_bottle import app02

root.mount(
\'app01\', app01.app01)
root.mount(
\'app02\', app02.app02)

root.run(host
=\'localhost\', port=8080)


总路由编辑

二级路由编辑



#!/usr/bin/env python
#
-*- coding:utf-8 -*-
from bottle import template, Bottle
app01
= Bottle()
@app01.route(
\'/hello/\', method=\'GET\')
def index():
return template(\'App01!\')


二级路由

 三、模板系统

 1)配置模板使用路径


import bottle
bottle.TEMPLATE_PATH.append(
\'./templates/\')

2)模板语言的常用方法

2.1)前后端结合

路由传值给前端模板



@root.route(\'/index/\',method="GET")
def index():
user_list
= [
{
\'id\':1,\'name\':\'root\',\'age\':18},
{
\'id\':1,\'name\':\'root\',\'age\':18},
{
\'id\':1,\'name\':\'root\',\'age\':18},
{
\'id\':1,\'name\':\'root\',\'age\':18},
]
return template(\'index.html\',name=\'superbody\',user_list=user_list)


python后端传值

前端调用值




"en">

"UTF-8">





    {{name}}
    % for item in user_list:
  • {{item[\'id\']}}-{{item[\'name\']}}

  • % end

    % laogao = "guaizi"
    {{laogao}}


{{get(\'age\',\'123\')}}




index.html

2.2)include 引用文件的标签



{{title}}



被引用的文件,tpl.html



"en">

"UTF-8">




% include(\'tpl.html\',title=\'搞事情\')



index.html调用tpl.html

2.3) rebase 引用文件的标签








{{
!base}}



base.html

导入基础模板


% rebase(\'base.tpl\', title=\'Page Title\')

Page Content ...


2.4)常用方法归纳


1、单值
2、单行Python代码
3、Python代码快
4、Python、Html混合

 示例



1、单值


{{name}}

2、单行Python代码


% s1 = "hello"


3、Python代码块


<%
# A block of python code
name
= name.title().strip()
if name == "Alex":
name
="seven"
%>


4、Python、Html混合



% if True:
{{name}}
% end

    % for item in name:
  • {{item}}

  • % end


html模板语音归纳

2.5)如果没有该值的情况下的默认值设置



# 检查当前变量是否已经被定义,已定义True,未定义False
defined(name)
# 获取某个变量的值,不存在时可设置默认值
get(name, default=None)

{{get(\'age\',\'123\')}}

# 如果变量不存在时,为变量设置默认值
setdefault(name,
default)

默认值

2.6){{ wupeiqi() }} 。定义函数,python后端定义函数,html前端调用函数执行



#!/usr/bin/env python
#
-*- coding:utf-8 -*-
from bottle import template, Bottle,SimpleTemplate
root
= Bottle()
def custom():
return \'123123\'
@root.route(
\'/hello/\')
def index():
# 默认情况下去目录:[
\'./\', \'./views/\']中寻找模板文件 hello_template.html
# 配置在 bottle.TEMPLATE_PATH 中
return template(\'hello_template.html\', name=\'alex\', wupeiqi=custom)
root.run(host
=\'localhost\', port=8080)


main.py




"en">
"UTF-8">



自定义函数


{{ wupeiqi() }}



hello_template.html

2.6.1){{ !wupeiqi() }}。渲染引用的html标签

被调用的python函数


def custom():
return \'

hello world

\'

前端使用



自定义函数


{{
!wupeiqi() }}

 2.7)替换模板。



from bottle import jinja2_template
@root.route(
\'/login/\',method=["POST","GET"])
def login():
return jinja2_template(\'login.html\')


替换模板

 jinja2_template模板与django模板使用一样

四、公共组件

1)request:Bottle中的request其实是一个LocalReqeust对象,其中封装了用户请求的相关信息



request.headers
请求头信息

request.query
get请求信息

request.forms
post请求信息

request.files
上传文件信息

request.
params
get和post请求信息

request.GET
get请求信息

request.POST
post和上传信息

request.COOKIEs
COOKIE信息

request.environ
环境相关相关


请求信息

2)response:Bottle中的request其实是一个LocalResponse对象,其中框架即将返回给用户的相关信息



response
response.status_line
状态行

response.status_code
状态码

response.headers
响应头

response.charset
编码

response.set_COOKIE
在浏览器上设置COOKIE

response.delete_COOKIE
在浏览器上删除COOKIE


View Code

五、Bottle支持的WSGI



server_names = {
\'cgi\': CGIServer,
\'flup\': FlupFCGIServer,
\'wsgiref\': WSGIRefServer,
\'waitress\': WaitressServer,
\'cherrypy\': CherryPyServer,
\'paste\': PasteServer,
\'fapws3\': FapwsServer,
\'tornado\': TornadoServer,
\'gae\': AppEngineServer,
\'twisted\': TwistedServer,
\'diesel\': DieselServer,
\'meinheld\': MeinheldServer,
\'gunicorn\': GunicornServer,
\'eventlet\': EventletServer,
\'gevent\': GeventServer,
\'geventSocketIO\':GeventSocketIOServer,
\'rocket\': RocketServer,
\'bjoern\' : BjoernServer,
\'auto\': AutoServer,
}


wsgi服务

使用时,只需在主app执行run方法时指定参数即可:



#!/usr/bin/env python
#
-*- coding:utf-8 -*-
from bottle import Bottle
root
= Bottle()

@root.route(
\'/hello/\')
def index():
return "Hello World"
# 默认server =
\'wsgiref\',性能最差,测试专用
root.run(host
=\'localhost\', port=8080, server=\'wsgiref\')


main.py

使用Python内置模块wsgiref,如果想要使用其他时,则需要首先安装相关类库,然后才能使用

六、数据库操作

可手写orm框架,或者pymysql使用

 

原文出处:https://www.cnblogs.com/wupeiqi/articles/5341480.html



推荐阅读
  • 本文由编程笔记小编整理,主要介绍了使用Junit和黄瓜进行自动化测试中步骤缺失的问题。文章首先介绍了使用cucumber和Junit创建Runner类的代码,然后详细说明了黄瓜功能中的步骤和Steps类的实现。本文对于需要使用Junit和黄瓜进行自动化测试的开发者具有一定的参考价值。摘要长度:187字。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • Linux的uucico命令使用方法及工作模式介绍
    本文介绍了Linux的uucico命令的使用方法和工作模式,包括主动模式和附属模式。uucico是用来处理uucp或uux送到队列的文件传输工具,具有操作简单快捷、实用性强的特点。文章还介绍了uucico命令的参数及其说明,包括-c或--quiet、-C或--ifwork、-D或--nodetach、-e或--loop、-f或--force、-i或--stdin、-I--config、-l或--prompt等。通过本文的学习,读者可以更好地掌握Linux的uucico命令的使用方法。 ... [详细]
  • 如何在php文件中添加图片?
    本文详细解答了如何在php文件中添加图片的问题,包括插入图片的代码、使用PHPword在载入模板中插入图片的方法,以及使用gd库生成不同类型的图像文件的示例。同时还介绍了如何生成一个正方形文件的步骤。希望对大家有所帮助。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • PHPMailer邮件类邮件发送功能的使用教学及注意事项
    本文介绍了使用国外开源码PHPMailer邮件类实现邮件发送功能的简单教学,同时提供了一些注意事项。文章涵盖了字符集设置、发送HTML格式邮件、群发邮件以及避免类的重定义等方面的内容。此外,还提供了一些与PHP相关的资源和服务,如传奇手游游戏源码下载、vscode字体调整、数据恢复、Ubuntu实验环境搭建、北京爬虫市场、进阶PHP和SEO人员需注意的内容。 ... [详细]
  • 延迟注入工具(python)的SQL脚本
    本文介绍了一个延迟注入工具(python)的SQL脚本,包括使用urllib2、time、socket、threading、requests等模块实现延迟注入的方法。该工具可以通过构造特定的URL来进行注入测试,并通过延迟时间来判断注入是否成功。 ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • EzPP 0.2发布,新增YAML布局渲染功能
    EzPP发布了0.2.1版本,新增了YAML布局渲染功能,可以将YAML文件渲染为图片,并且可以复用YAML作为模版,通过传递不同参数生成不同的图片。这个功能可以用于绘制Logo、封面或其他图片,让用户不需要安装或卸载Photoshop。文章还提供了一个入门例子,介绍了使用ezpp的基本渲染方法,以及如何使用canvas、text类元素、自定义字体等。 ... [详细]
  • Python操作MySQL(pymysql模块)详解及示例代码
    本文介绍了使用Python操作MySQL数据库的方法,详细讲解了pymysql模块的安装和连接MySQL数据库的步骤,并提供了示例代码。内容涵盖了创建表、插入数据、查询数据等操作,帮助读者快速掌握Python操作MySQL的技巧。 ... [详细]
author-avatar
小女人的忧伤--
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有