Flask

0、陷阱  自己的文件名不要叫flask.py,会导致from flask失效,它分不清楚那个是flask库

1、例子

# coding=utf8
from flask import Flask,url_for,render_template,request
from werkzeug import secure_filename

app=Flask(__name__)
@app.route('/app/')
def hello1():
  return 'hello world '+request.args.get('user')


@app.route('/commit/<name>')
def commit(name):
  print 'func hello1 is route:', url_for('hello1')
  return "%s" % name

print secure_filename('../../../../home/username/.hashrc')
if __name__=='__main__':
  app.run()


from flask import Flask,url_for,render_template,request
from werkzeug import secure_filename

app=Flask(__name__)
@app.route('/app/')
def hello1():
  return 'hello world'+url_for('show_post',post_id=5)

@app.route('/commit/<name>')
def commit(name):
  return "%s" % name

@app.route('/post/<int:post_id>')
def show_post(post_id):
  # show the post with the given id, the id is an integer
  return 'Post %d' % post_id

@app.route('/static/<filename>')
def getfile(filename):
  return app.send_static_file(filename) #注意,静态文件必须放在static目录下
 
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
  return render_template('hello.html', name=name) #注意,模板文件必须放在templates目录下。 curl -F "the_file=@a.txt" 127.0.0.1:5000/upload

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
  if request.method == 'POST':
    f = request.files['the_file']
    f.save("d:/" + secure_filename(f.filename))
    return f.filename+' upload ok!'
              
if __name__=='__main__':
  app.run()

hello.html
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello World!</h1>
{% endif %}


2、

from flask import Flask, session, redirect, url_for, escape, request

app = Flask(__name__)

@app.route('/')  #curl -s  --cookie session=eyJ1c2o http://127.0.0.1:5000/
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'

if __name__=='__main__':
  app.run()

WSGI是什么?

WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。自从 WSGI 被开发出来以后,许多其它语言中也出现了类似接口。

WSGI 的官方定义是,the Python Web Server Gateway Interface。从名字就可以看出来,这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。

WSGI 是作为 Web 服务器与 Web 应用程序或应用框架之间的一种低级别的接口,以提升可移植 Web 应用开发的共同点。WSGI 是基于现存的 CGI 标准而设计的。

很多框架都自带了 WSGI server ,比如 Flask,webpy,Django、CherryPy等等。当然性能都不好,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server或者是联合 nginx 做 uwsgi 。

WSGI 的设计确实参考了 Java 的 servlet。http://www.python.org/dev/peps/pep-0333/ 有这么一段话:

By contrast, although Java has just as many web application frameworks available, Java's "servlet" API makes it possible for applications written with any Java web application framework to run in any web server that supports the servlet API.

参考1  WSGI接口-廖雪峰

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832689740b04430a98f614b6da89da2157ea3efe2000

参考2  如何部署简单python + flask应用

http://www.jianshu.com/p/bd1d13ef4544


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值