/usr/bin/env python#-*- coding:utf-8 -*-frombottleimporttemplate, Bottle app02=Bottle() @app02.route('/hello/', method='GET')defindex():returntemplate('App02!') app02.py #!/usr/bin/env python#-*- coding:utf-8 -*-frombottleimporttemplate, Bottlefrombottleimportstatic_file app=Bot...
from bottle import route, static_file, run @route('/static/<filename:path>') def serve_static(filename): return static_file(filename, root='/path/to/static/files') run(host='localhost', port=8080) 上面的示例代码定义了一个静态文件服务路由/static/<filename:path>,将静态文件从指定目录中返...
frombottleimportstatic_file@route('/static/<filename>')defserver_static(filename):returnstatic_file(filename, root='/path/to/your/static/files') 错误页面 如果出了什么问题, Bottle 会显示一个信息丰富但相当简单的错误页面。您可以使用error()装饰: frombottleimporterror@error(404)deferror404(error):...
frombottleimportstatic_file@app.route('/downloads/<filename>')defdownload(filename):returnstatic_file(filename,root='./uploads',download=filename) 1. 2. 3. 4. 5. 代码解析: static_file:根据文件名提供文件下载,root='./uploads'表示文件存放的目录。 第五步:启动Bottle应用并测试 最后,我们需要...
Bottle是一个超轻量级的python库。说是库,其本身只由一个4000行左右的文件构成,并且不需要任何依赖,只靠python标准库即可运作。 和它本身的轻便一样,Bottle库的使用也十分简单。相信在看到本文前,读者对python也已经有了简单的了解。那么究竟何种神秘的操作,才能用百行代码完成一个服务器的功能?让我们拭目以待。 一...
这几天想用bottle来做一个简单的基于web页面的小应用,在调用显示静态文件时被路径卡了半天,现在把问题和解决办法写出来备用和分享给有需要的人。 先上代码: from bottle importstatic_file,route,run,TEMPLATE_PATH TEMPLATE_PATH.insert(0,'./testdir/') ...
这里我们可以看到一个新的函数static_file,第一个参数为文件名,第二个参数为根目录地址(即这个文件所在的位置),当前文件系统为: --HelloWorld.py --store1.txt 访问浏览器得到 当然你也可以把文件放在文件夹里,只要把root参数更换成文件夹的地址即可。
Bottle可以用于提供静态文件服务,如CSS、Java、图片等,方便前端页面的展示和引用。 from bottle import route, static_file, run @route('/static/<filename:path>') def serve_static(filename): return static_file(filename, root='/path/to/static/files') run(host='localhost', port=8080) ...
'/say/<name:re:[a-z]+>')def callback(name):return template('Hello {{name}}!')@app.route('/static/<path:path>')def callback(path):return static_file(path, root='static')1.3请求⽅法路由 @app.route('/hello/', method='POST') # 等同于@app.post('/hello/')
return static_file(path, ...) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 实现自己的过滤器 app = Bottle() def list_filter(config): ''' Matches a comma separated list of numbers. ''' delimiter = config or ',' regexp = r'\d+(%s\d)*' % re.escape(delimiter) ...