使用管理员身份打开cmd
conda create -n bottle python
conda activate bottle
pip install bottle
1.最少代码的服务器程序
from bottle import run,route
@route('/')
def index():
return "I am bottle"
run(host="localhost",port="80",debug=True,deloader=True)
2.使用面向对象的服务器程序框架
from bottle import Bottle,run
web=Bottle()
@web.route("/")
def index():
return "I am bottle2"
run(web)
服务器运行函数run,指定服务器的运行IP和端口(host,port);显示调试信息(debug);代码修改后自动重启服务(deloader)
1.默认响应get请求,可以定义method响应多种请求方法,request.method判断请求方式
from bottle import route, run ,request,get,post
htmlstr="""
BOTTLE
"""
@route("/")
def index():
return htmlstr
@route("/demo_post",method="POST")#1.响应post请求
def pt():
return "receive method post"
#@route("/demo_gp",method=["POST","GET"])#2.响应两种请求方式
@get("/demo_gp")#3.响应两种请求方法
@post("/demo_gp")
def gp():
if request.method=="GET":#request.method判断请求的方式
return htmlstr
else:
return "this is post"
run()
2.动态路由
from bottle import route,run ,Bottle
import re
@route("/hello/" )#1.路由传递字符串
def index(name=""):
return "Hello %s"%name
@route("/uid/" )#2.路由传递整型
def getuid(uid=0):
return "your id is %d"%uid
@route("/weight/" )#3.路由传递浮点数
def getweight(weight):
return "your weight:%f"%weight
@route("/getfilepath/" )#4.路由传递路径
def getpath(file_path):
return file_path
@route("/rege/" )#5.正则匹配路由传递的参数
def rege(ret):
return ret
#6.自定义路由过滤器
app=Bottle()
def list_filter(config):
delimiter=config or ","
regexp=r'\d+(%s\d)*'%re.escape(delimiter)
def to_python(match):
return map(int,match.split(delimiter))
def to_url(numbers):
return delimiter.join(map(str,numbers))
return regexp,to_python,to_url
app.router.add_filter('idslist',list_filter)
@app.route("/hello/" )
def hello(ids):
res=''
for i in ids:
res+=str(i)+'-'
return "hello %s"%res
run(app,port=80,debug=True)
from bottle import route,run,static_file,error,abort,redirect
#1.返回静态文件内容return static_file(filename,root="静态文件地址",mimetype="静态文件类型")
@route("/static/" )
def index(filename):
return static_file(filename,root="",download=True)
#2.强制下载文件,return static_file(filename,root="",download=True/"yourfilename")
@route("/dl/" )
def dl(filename):
return static_file(filename,root='',download="sd.jpg")
#3.指定404页面
@error(404)
def err(err):
return "页面丢失"
#4.url转向:转向错误abort(404,"error_info"),调用redirect("other url")
@route("/abort")
def ab():
abort(404,"转向丢失页面")##4.1转向丢失页面
##4.2转向指定页面
@route("/")
def index():
return "登录成功"
@route("/login/" )###登陆成功转向/目录,失败就转向login,且页面丢失
def login(name):
if name =="abc":
redirect("/")
else:
redirect("/login")
run(port="80",debug=True,reloader=True)
1.get参数提供:url链接添加”?名称=值&名称=值…“
get 参数获取:request.query.name(当name不存在时,返回空字符串)
from bottle import route, run ,request,get,post
htmlstr="""
BOTTLE
提交get信息
"""
@route("/")
def index():
return htmlstr
@route("/get_to")
def getto():
name=request.query.name#获取
telephone=request.query.telephone
email=request.query.email
return (name,telephone,email)
run(debug=True,reloader=True)
2.post参数提供:在提交的表单中添加method=“post”
post 参数获取:request.forms.get(“name”)
from bottle import route, run ,request,get,post
htmlstr="""
BOTTLE
"""
@route("/")
def index2():
return htmlstr
@route("/post_to",method="POST")#post方式接受
def postto():
name=request.forms.get('name')#post方式提取参数
telephone=request.forms.get('telephone')
email=request.forms.get('email')
return (name,telephone,email)
run(debug=True,reloader=True)
from bottle import route,run,Response
@route("/")
def index():
#return {"a":"djn","b":45,"c":2.3}#1.返回字典
#return ()#2.返回空
# dec="abcd"
# return dec.encode("utf-8") #3.返回字节串
#return ("sdf","sdf")4.#返回元组或列表,不能返回嵌套的
#5.Response.charset或者Response.content_type指定返回字节编码
#Response.charset="gbk"
#Response.content_type="text/html;charset=gbk"
return "你好"
run(port="80",debug=True,reloader=True)