【脚本语言系列】关于PythonWeb服务器Bottle,你需要知道的事

如何使用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文件,非常易于使用和部署。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值