1. 程式人生 > >Python之Bottle框架使用

Python之Bottle框架使用

本文主要包含的內容是Bottle框架介紹和安裝使用。

一、Bottle框架介紹

Bottle是一個快速小巧,輕量級的 WSGI 微型 web 框架。同時Bottle也是一個簡單高效的遵循WSGI的微型python Web框架。

說微型,是因為它只有一個檔案,除Python標準庫外,它不依賴於任何第三方模組。

URL對映(Routing):將 URL 請求對映到 Python 函式,使 URL 更簡潔。

模板(Templates):快速且 pythonic 的內建模板引擎 ,同時支援 mako, jinja2 和 cheetah 等模板。

基礎功能(Utilities):方便地訪問表單資料,上傳檔案,使用 cookie,檢視 HTTP 元資料。

開發伺服器(Server):內建了開發伺服器,且支援 paste, fapws3 , bjoern, Google App Engine,cherrypy 等符合 WSGI 標準的 HTTP 伺服器。

 

官網地址為: http://www.bottlepy.org/docs/dev/index.html

官網教程: http://www.bottlepy.org/docs/dev/tutorial.html

 

二、安裝和使用

本地Windows安裝使用pip install bottle即可

Linux系統可以通過這種形式安裝 sudo apt-get install pip-bottle

簡單示例程式碼(hello.py):

from bottle import route, run

@route('/hello')
def hello():
    return "Hello World!"

run(host='localhost', port=8080, debug=True)

 

執行效果如圖: