如何使用bottle
欢迎页面
# -*- coding:utf-8 -*-
from bottle import route, run
@route("/")
def home():
return "Hello AllenMoore!"
run(host="localhost", port=9999, debug=True)
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://localhost:9999/
Hit Ctrl-C to quit.
127.0.0.1 - - [22/Jun/2017 10:26:00] "GET / HTTP/1.1" 200 17
127.0.0.1 - - [22/Jun/2017 10:44:10] "GET / HTTP/1.1" 200 17
静态网页
# -*- coding:utf-8 -*-
from bottle import route, run, static_file
@route("/")
def main():
return static_file("temp.html", root=".")
run(host="localhost", port=9999)
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://localhost:9999/
Hit Ctrl-C to quit.
127.0.0.1 - - [22/Jun/2017 10:46:17] "GET / HTTP/1.1" 200 62
动态网页
# -*- coding:utf-8 -*-
from bottle import route, run, static_file
@route("/")
def main():
return static_file("temp.html", root=".")
@route("/echo/<pagename>")
def echo(pagename):
return "Hello %s!" %pagename
run(host="localhost", port=9999)
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://localhost:9999/
Hit Ctrl-C to quit.
127.0.0.1 - - [22/Jun/2017 10:47:27] "GET /favicon.ico HTTP/1.1" 404 742
127.0.0.1 - - [22/Jun/2017 10:47:28] "GET / HTTP/1.1" 304 0
127.0.0.1 - - [22/Jun/2017 10:47:49] "GET /echo/AllenMoore HTTP/1.1" 200 17
测试效果
# -*- coding:utf-8 -*-
import requests
resp = requests.get("http://localhost:9999/echo/AllenMoore")
if resp.status_code == 200 and resp.text == "Hello AllenMoore!":
print "Got Good News!"
else:
print "Got Bad News: %s" %resp.text
Got Good News!
什么是bottle
Bottle是简单的Web服务器。
为何使用bottle
Bottle有且只有一个简单的Python文件,非常易于使用和部署。