热门标签 | 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



推荐阅读
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • 本文由编程笔记小编整理,主要介绍了使用Junit和黄瓜进行自动化测试中步骤缺失的问题。文章首先介绍了使用cucumber和Junit创建Runner类的代码,然后详细说明了黄瓜功能中的步骤和Steps类的实现。本文对于需要使用Junit和黄瓜进行自动化测试的开发者具有一定的参考价值。摘要长度:187字。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
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社区 版权所有