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

来源:互联网 发布:directx9安装需要网络 编辑:程序博客网 时间:2024/05/29 12:02

如何使用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 17127.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!" %pagenamerun(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 742127.0.0.1 - - [22/Jun/2017 10:47:28] "GET / HTTP/1.1" 304 0127.0.0.1 - - [22/Jun/2017 10:47:49] "GET /echo/AllenMoore HTTP/1.1" 200 17

测试效果

# -*- coding:utf-8 -*-import requestsresp = 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
原创粉丝点击