为什么使用bottle?因为简单,就一个py文件,和其他模块没有依赖,3000多行代码。

http://www.bottlepy.org/docs/dev/

既然开始学习,就安装它吧。

pip3 install bottle

ok

第一个代码:

from bottle import route,run,template

@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!',name=name) run(host='localhost',port=8080)

运行ok

从这段代码可以i看出来,bottle虽小,支持不差,包括route,template等,都支持。

而最后一行代码的run,实际上,就是启动一个内置的web server。3000多行代码,还包括一个这个,厉害。

处于简单的目的,我们后续学习,大部分时候,都是用一个module-level的装饰器route()去定义路由。这会把routes都加入到一个全局的缺省程序,当第一次调用route()的时候,Bottle的一个进程会被创建。

实际上,更应该代码是这样的;

from bottle import Bottle, run
app = Bottle()
@app.route('/hello')
def hello():
return "Hello World!"
run(app, host='localhost', port=8080)

上面的,route基本都是静态的,下面,我们介绍动态路由:

动态路由相对静态路由来说,用的更广泛,一次性匹配更多的url:

比如/hello/<name>可以匹配:/hello/wcf /hello/hanyu /hello/whoami 但是不匹配:/hello /hello/ /hello/wcf/ddd

举例如下;

@route('/wiki/<pagename>') # matches /wiki/Learning_Python
def show_wiki_page(pagename):
...
@route('/<action>/<user>') # matches /follow/defnull
def user_api(action,

这里,还需要关注动态路由的Filters(我称之为匹配器,目前有4种,可以增加更多),具体是:

:int matches (signed) digits only and converts the value to integer.
• :float similar to :int but for decimal numbers.
• :path matches all characters including the slash character in a non-greedy way and can be used to match more
than one path segment.
• :re allows you to specify a custom regular expression in the config field. The matched value is not modified

举例

from flask import Flask

app = Flask(__name__)

@app.route('/user/<int:user_id>'
def user(user_id):
return 'Hello,%d' %user_id if __name__ == '__main__':
app.run(debug=True)
#coding:utf-8

from flask import Flask
from werkzeug.routing import BaseConverter #定义正则转换器的类
class RegexConverter(BaseConverter):
def __init__(self,url_map,*items):
super(RegexConverter, self).__init__(url_map)
self.regex=items[0] app = Flask(__name__)
#实例化
app.url_map.converters['regex']=RegexConverter @app.route('/user/<regex("([a-z]|[A-Z]){4}"):username>', methods=['POST', 'GET'])
def user(username):
return 'Hello,%s' % username if __name__ == '__main__':
app.run(debug=True)

代码如下;

使用bottle进行web开发(1):hello world的更多相关文章

  1. 使用bottle进行web开发(9):文件上传;json传递

    1.文件上传 如果要完成文件上传,则需要对上文的form做一点改动,具体如下: <form action="/upload" method="post" ...

  2. 使用bottle进行web开发(8):get的参数传递,form里的额数据传递等

    1.诸如:forum?id=1&page=5这样的,在bottle里,可以通过request.query来访问这些值,举例如下: from bottle import Bottle,run,r ...

  3. 使用bottle进行web开发(5):Generating Content

    在纯粹的 WSGI中,你的应用能返回的数据类型是十分有限的,你必须返回可迭代的字符串,你能返回字符串是因为字符串是可以迭代的,但是这导致服务器将你的内容按一字符一字符的传送,这个时候,Unicode ...

  4. 使用bottle进行web开发(4):HTTPError

    from bottle import error @error(404) def error404(error): return 'Nothing here, sorry' 上述代码,是对404的定义 ...

  5. 使用bottle进行web开发(2):http request

    我们知道,http request有多个方法,比如get,post,delete,patch,put等.对用的,bottle都定义了相应的装饰器,目前定义了五个: get(),post(),put() ...

  6. 使用bottle进行web开发(6):Response 对象

    Response的元数据(比如http的status code,headers,cookies等,都被i封装到一个叫Response的对象中,并传给浏览器. status code:status co ...

  7. 使用bottle进行web开发(3):静态文件的获取

    静态文件(比如css啊,需要下载的各位文件等),需要通过static_file来操作,首先记得要在import中导入 @route('/static/<filename>') def se ...

  8. python bottle框架(WEB开发、运维开发)教程

    教程目录 一:python基础(略,基础还是自己看书学吧) 二:bottle基础 python bottle web框架简介 python bottle 框架环境安装 python bottle 框架 ...

  9. bottle+cherrypy快速开发web服务

    我目前用得最顺手的python web框架是bottle,简单方便. bottle有一个开发用的http服务器,效率不高,单线程,阻塞. 所以,得找个别的服务器来部署. 根据bottle官方的文档,发 ...

随机推荐

  1. 6,MongoDB 之 Array Object 的特殊操作

    相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...

  2. build dynamic libraries for iOS and load them at runtime

    编译了libmt.dylib, 和 test 程序调用,均正常.在xcode中显示调用正常,隐式调用则出现问题. 提示 dyld: Library not loaded. 即使存在在/usr/lib/ ...

  3. android 管理Touch事件

    The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its child ...

  4. SQL语句中的换行符

    直接看示例(顺便注意下连接符) 示例1:Access INSERT INTO ZD_DJDCB (DJH,ZDSZ) VALUES ('150105101204JC00428', '北: 地块1' + ...

  5. 孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录

    孤荷凌寒自学python第五十二天初次尝试使用python读取Firebase数据库中记录 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数 ...

  6. Leetcode 667.优美的排列II

    优美的排列II 给定两个整数 n 和 k,你需要实现一个数组,这个数组包含从 1 到 n 的 n 个不同整数,同时满足以下条件: ① 如果这个数组是 [a1, a2, a3, ... , an] ,那 ...

  7. iPhone新建项目不能全屏

    上个周做项目的时候,发现新建了一个项目不能全屏.伤透了我的脑筋,然后又请教了团队里其他两个大牛帮我搞定了这个问题. 虽然是搞定了,但也看的出大牛也是云里雾里.歪打正着解决的. 今天又想新做个项目,这个 ...

  8. android系统联系人分组特效实现(1)---分组导航和挤压动画

    1.打开activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr ...

  9. spring 配置问题记录1-@ResponseBody和favorPathExtension

    在搭建springmvc+easyui的项目时,有一个地方参照网上说的方法一直没实现出来, 就是前台的datagrid的数据渲染不上去, 尝试了好多种方法,包括也找了目前手里的项目来进行比较,也没发现 ...

  10. Linux运维文档之nginx

    NGINX安装配置1.检查并且安装依赖组件检查安装nginx的依赖性,nginx的模块需要第三方库的支持,检查是否安装下列库:zlib.zlib-devel.openssl.openssl-devel ...