1 2 3 4 | pip install bottle easy_install bottle apt-get install python-bottle wget http://bottlepy.org/bottle.py |
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/env python # -*- coding:utf-8 -*- from bottle import template, Bottle root = Bottle() @root.route('/hello/') def index(): return "Hello World" # return template('<b>Hello {{name}}</b>!', name="Alex") root.run(host='localhost', port=8080) |
1 2 3 | @root.route('/hello/') def index(): return template('<b>Hello {{name}}</b>!', name="Alex") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @root.route('/wiki/<pagename>') def callback(pagename): ... @root.route('/object/<id:int>') def callback(id): ... @root.route('/show/<name:re:[a-z]+>') def callback(name): ... @root.route('/static/<path:path>') def callback(path): return static_file(path, root='static') |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @root.route('/hello/', method='POST') def index(): ... @root.get('/hello/') def index(): ... @root.post('/hello/') def index(): ... @root.put('/hello/') def index(): ... @root.delete('/hello/') def index(): ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/env python # -*- coding:utf-8 -*- from bottle import template, Bottle from bottle import static_file root = Bottle() @root.route('/hello/') def index(): return template('<b>Root {{name}}</b>!', name="Alex") from framwork_bottle import app01 from framwork_bottle import app02 root.mount('app01', app01.app01) root.mount('app02', app02.app02) root.run(host='localhost', port=8080) |
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/env python # -*- coding:utf-8 -*- from bottle import template, Bottle root = Bottle() @root.route('/hello/') def index(): # 默认情况下去目录:['./', './views/']中寻找模板文件 hello_template.html # 配置在 bottle.TEMPLATE_PATH 中 return template('hello_template.tpl', name='alex') root.run(host='localhost', port=8080) |
1 2 3 4 5 | # 导入其他模板文件 % include('header.tpl', title='Page Title') Page Content % include('footer.tpl') |
1 2 3 4 | # 导入母版 % rebase('base.tpl', title='Page Title') <p>Page Content ...</p> |
1 | # 检查当前变量是否已经被定义,已定义True,未定义False |
1 | # 获取某个变量的值,不存在时可设置默认值 |
1 | # 如果变量不存在时,为变量设置默认值 |
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/env python # -*- coding:utf-8 -*- from bottle import Bottle root = Bottle() @root.route('/hello/') def index(): return "Hello World" # 默认server ='wsgiref' root.run(host='localhost', port=8080, server='wsgiref') |
1 | pip install Flask |
1 2 3 4 5 6 7 8 9 | from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() |
1 2 3 4 5 6 7 8 9 | DEFAULT_CONVERTERS = { 'default': UnicodeConverter, 'string': UnicodeConverter, 'any': AnyConverter, 'path': PathConverter, 'int': IntegerConverter, 'float': FloatConverter, 'uuid': UUIDConverter, } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Flask,render_template app = Flask(__name__) def wupeiqi(): return '<h1>Wupeiqi</h1>' @app.route('/login', methods=['GET', 'POST']) def login(): return render_template('login.html', ww=wupeiqi) app.run() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | request.method request.args request.form request.values request.files request.cookies request.headers request.path request.full_path request.script_root request.url request.base_url request.url_root request.host_url request.host |
1 2 3 | @app.route('/index/', methods=['GET', 'POST']) def index(): return "index" |
1 2 3 4 5 6 7 8 | from flask import Flask,render_template,request app = Flask(__name__) @app.route('/index/', methods=['GET', 'POST']) def index(): return render_template("index.html") app.run() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/env python # -*- coding:utf-8 -*- from flask import Flask, redirect, url_for app = Flask(__name__) @app.route('/index/', methods=['GET', 'POST']) def index(): # return redirect('/login/') return redirect(url_for('login')) @app.route('/login/', methods=['GET', 'POST']) def login(): return "LOGIN" app.run() |
1 2 3 4 5 6 7 8 9 10 11 12 | from flask import Flask, abort, render_template app = Flask(__name__) @app.route('/index/', methods=['GET', 'POST']) def index(): return "OK" @app.errorhandler(404) def page_not_found(error): return render_template('page_not_found.html'), 404 app.run() |
1 2 3 4 5 6 7 8 9 10 11 12 13 | from flask import Flask, abort, render_template,make_response app = Flask(__name__) @app.route('/index/', methods=['GET', 'POST']) def index(): response = make_response(render_template('index.html')) # response是flask.wrappers.Response类型 # response.delete_cookie # response.set_cookie # response.headers['X-Something'] = 'A value' return response app.run() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from flask import Flask, session, redirect, url_for, escape, request app = Flask(__name__) @app.route('/') def index(): if 'username' in session: return 'Logged in as %s' % escape(session['username']) return 'You are not logged in' @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': session['username'] = request.form['username'] return redirect(url_for('index')) return ''' <form action="" method="post"> <p><input type=text name=username> <p><input type=submit value=Login> </form> ''' @app.route('/logout') def logout(): # remove the username from the session if it's there session.pop('username', None) return redirect(url_for('index')) # set the secret key. keep this really secret: app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from flask import Flask, flash, redirect, render_template, request app = Flask(__name__) app.secret_key = 'some_secret' @app.route('/') def index1(): return render_template('index.html') @app.route('/set') def index2(): v = request.args.get('p') flash(v) return 'ok' if __name__ == "__main__": app.run() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from flask import Flask, flash, redirect, render_template, request app = Flask(__name__) app.secret_key = 'some_secret' @app.route('/') def index1(): return render_template('index.html') @app.route('/set') def index2(): v = request.args.get('p') flash(v) return 'ok' class MiddleWare: def __init__(self,wsgi_app): self.wsgi_app = wsgi_app def __call__(self, *args, **kwargs): return self.wsgi_app(*args, **kwargs) if __name__ == "__main__": app.wsgi_app = MiddleWare(app.wsgi_app) app.run(port=9999) |
1 2 3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") application = tornado.web.Application([ (r"/index", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") class StoryHandler(tornado.web.RequestHandler): def get(self, story_id): self.write("You requested the story " + story_id) class BuyHandler(tornado.web.RequestHandler): def get(self): self.write("buy.wupeiqi.com/index") application = tornado.web.Application([ (r"/index", MainHandler), (r"/story/([0-9]+)", StoryHandler), ]) application.add_handlers('buy.wupeiqi.com$', [ (r'/index',BuyHandler), ]) if __name__ == "__main__": application.listen(80) tornado.ioloop.IOLoop.instance().start() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.render('home/index.html') settings = { 'template_path': 'template', } application = tornado.web.Application([ (r"/index", MainHandler), ], **settings) if __name__ == "__main__": application.listen(80) tornado.ioloop.IOLoop.instance().start() |
写cookie过程:
- 将值进行base64加密
- 对除值以外的内容进行签名,哈希算法(无法逆向解析)
- 拼接 签名 + 加密值
读cookie过程:
- 读取 签名 + 加密值
- 对签名进行验证
- base64解密,获取值内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web from hashlib import sha1 import os, time import re class MainForm(object): def __init__(self): self.host = "(.*)" self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" self.port = '(\d+)' self.phone = '^1[3|4|5|8][0-9]\d{8}$' def check_valid(self, request): form_dict = self.__dict__ for key, regular in form_dict.items(): post_value = request.get_argument(key) # 让提交的数据 和 定义的正则表达式进行匹配 ret = re.match(regular, post_value) print key,ret,post_value class MainHandler(tornado.web.RequestHandler): def get(self): self.render('index.html') def post(self, *args, **kwargs): obj = MainForm() result = obj.check_valid(self) self.write('ok') settings = { 'template_path': 'template', 'static_path': 'static', 'static_url_prefix': '/static/', 'cookie_secret': 'aiuasdhflashjdfoiuashdfiuh', 'login_url': '/login' } application = tornado.web.Application([ (r"/index", MainHandler), ], **settings) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start() |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |