web框架--bottle
大亮头
2024-11-08 02:13:45
原文
2
3
4
pip install bottle
easy_install bottle
apt-get install python-bottle
wget http://bottlepy.org/bottle.py
|
bottle介绍
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle
root = Bottle()
@root.route('/hello/')
def index():
return "Hello World"
# return template('<b>Hello {{name}}</b>!', name="Alex")
root.run(host='localhost', port=8080)
|
具体介绍
一、路由系统
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@root.route('/wiki/<pagename>') #<>号是规定格式,
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@root.route('/hello/', method='POST')
def index():
...
@root.get('/hello/') #代表只接收get请求
def index():
...
@root.post('/hello/')
def index():
...
@root.put('/hello/')
def index():
...
@root.delete('/hello/')
def index():
...
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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('<b>Root {{name}}</b>!', name="Alex")
from framwork_bottle import app01
from framwork_bottle import app02
root.mount('app01', app01.app01) #挂载之后,app01开头的都会交有app01.app01对象去处理
root.mount('app02', app02.app02)
root.run(host='localhost', port=8080)
|
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import template, Bottle
app01 = Bottle()
@app01.route('/hello/', method='GET')
def index():
return template('<b>App01</b>!')
app01.py
|
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
request.headers
HTTP请求头信息 (ip,浏览器......)
request.query
get请求信息 /?page=123
request.forms
post请求信息
request.files
上传文件信息
request.params
get和post请求信息
request.GET
get请求信息
request.POST
post和上传信息
request.cookies
cookie信息
request.environ
环境相关相关 #上面没有的都在这里
|
2、响应相关内容:response
当开发人员的代码处理完用户请求之后,会将其执行内容相应给用户,相应的内容会封装在Bottle的response中,然后再由框架将内容返回给用户
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
response
response.status_line
状态行
response.status_code
状态码
response.headers
响应头
response.charset
编码
response.set_cookie
在浏览器上设置cookie
response.delete_cookie
在浏览器上删除cookie
|
所以,公共组件本质其实就是为开发人员提供接口,使其能够获取用户信息并配置响应内容。
四、服务
对于Bottle框架其本身未实现类似于Tornado自己基于socket实现Web服务,所以必须依赖WSGI,默认Bottle已经实现并且支持的WSGI有:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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,
}
|
使用时,只需在主app执行run方法时指定参数即可:
|
1
2
3
4
5
6
7
8
9
10
|
#!/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')
|
默认server="wsgiref",即:使用Python内置模块wsgiref,如果想要使用其他时,则需要首先安装相关类库,然后才能使用。如:
|
1
2
3
4
5
6
7
8
9
10
11
|
# 如果使用Tornado的服务,则需要首先安装tornado才能使用
# bottle.py源码
class TornadoServer(ServerAdapter):
""" The super hyped asynchronous server by facebook. Untested. """
def run(self, handler): # pragma: no cover
# 导入Tornado相关模块
import tornado.wsgi, tornado.httpserver, tornado.ioloop
container = tornado.wsgi.WSGIContainer(handler)
server = tornado.httpserver.HTTPServer(container)
server.listen(port=self.port,address=self.host)
tornado.ioloop.IOLoop.instance().start()
|
PS:以上WSGI中提供了19种,如果想要使期支持其他服务,则需要扩展Bottle源码来自定义一个ServerAdapter
更多参见:http://www.bottlepy.org/docs/dev/index.html
- 微型 Python Web 框架 Bottle - Heroin blog
微型 Python Web 框架 Bottle - Heroin blog 微型 Python Web 框架 Bottle
- 轻量的web框架Bottle
简洁的web框架Bottle 简介 Bottle是一个非常简洁,轻量web框架,与django形成鲜明的对比,它只由一个单文件组成,文件总共只有3700多行代码,依赖只有python标准库.但是麻雀虽 ...
- Python Web框架 bottle flask
Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. 1 2 3 4 pip instal ...
- python web框架(bottle,flask,tornado)
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. pip i ...
- Web框架们
Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...
- Python之路【第十八篇】:Web框架们
Python之路[第十八篇]:Web框架们 Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Pytho ...
- Python之Web框架们
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. pip i ...
- Django:之不得不说的web框架们
python的web框架 Bottle Bpttle是一个快速.简洁.轻量级的基于WSIG的微型web框架,此框架只有一个.py文件,除了python的标准库外,其不依赖任何其它模块. pip ins ...
- web框架和django基础(粗糙版)
web框架本质: 浏览器:socket客户端 服务器:socket服务端 1.自己写socket服务端(最傻) #!/usr/bin/env python ...
随机推荐
- javascript Xml兼容性随笔
一.前言 (function (window) { if (!window.jasen) { window.jasen = {}; } if (!window.jasen.core) { window ...
- 【原创】WinForm 模态对话框
今天解决的一个问题,记录下,以备后用. 问题描述:WinForm程序有超时自动退出功能,但是有些模态对话框不关掉的话会退出失败,原因(猜测): 程序倒计时用的System.Windows.Forms. ...
- tcp/udp高并发和高吐吞性能测试工具
在编写一个网络服务的时候都比较关心这个服务能达到多少并发连接,而在这连接的基础上又能达到一个怎样的交互能力.编写服务已经是一件很花力气的事情,而还要去编写一个能够体现结果的测试工具就更加消耗工作时间. ...
- MQTT V3.1--我的理解
最近因为工作需要,需要对推送消息了解,因此对MQTT进行了整理,这里更多的是对MQTT英文版的翻译和理解. MQTT(Message Queue Telemetry Transport),遥测传输协议 ...
- objective-c(内存管理)
本文主要记录objective-c 内存管理的知识点: 1.objective-c的对象都是分配内存在堆上,与C的mallock和C++的new类似,只有int等系统变量分配内存在栈上: 2.obje ...
- STC12C5A60S2笔记6(中断)
1. 基本特性 1) 中断源 STC12C5A60S2共有十个中断源,每个中断源可设置4类优先级:当相同优先级下各中断优先级由高到低依次如下: 1.1)INT0(外部中断0) 中断向量地址 0003H ...
- sublime 2/3 for mac link to command
Sublime command for mac : execute those command: subl 2: sudo ln -s /Applications/Sublime\ Text\ 2.a ...
- Silverlight无法启动调试,错误“Unable to start debugging. The Silverlight Developer Runtime is not installed. Please install a matching version.” 解决办法
今天调试Silverlight出现了以下错误: 意思是“无法启动调试,因为Silverlight Developer Runtime没有安装,请安装一个匹配的版本”.但是按Ctrl + F5可以调试运 ...
- Git学习笔记(7)——多人协作
本文主要记录了,多人协作时,产生冲突时的解决情况. 多人环境创建 首先我们需要模拟一个多人环境.前面的Git的学习都是在Ubuntu上面,现在我们也搭建一个win环境吧.安装win环境下的Git,很简 ...
- Redis主从复制问题和扩容问题的解决思路
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/106.html?1455867541 一.解决主从复制问题 当使用Redi ...