Flask
- #environ:一个包含所有HTTP请求信息的dict对象
- #start_response:一个发送HTTP响应的函数
- def application(environ, start_response):
- method = environ['REQUEST_METHOD']
- path = environ['PATH_INFO']
- #if method=='GET' and path=='/':
- # return handle_home(environ, start_response)
- start_response('200 OK', [('Content-Type', 'text/html')])
- #[1:] 切片 第2个字符到后面所有字符
- body = '<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')
- return [body.encode('utf-8')]
- #return [b'<h1>Hello, web!</h1>']
- # 从wsgiref模块导入:
- from wsgiref.simple_server import make_server
- # 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
- httpd = make_server('', 8000, application)
- print('Serving HTTP on port 8000...')
- # 开始监听HTTP请求:
- httpd.serve_forever()
- from flask import Flask
- from flask import request
- app = Flask(__name__)
- @app.route('/', methods=['GET', 'POST'])
- def home():
- return '<h1>Home</h1>'
- @app.route('/signin', methods=['GET'])
- def signin_form():
- return '''<form action="/signin" method="post">
- <p><input name="username"></p>
- <p><input name="password" type="password"></p>
- <p><button type="submit">Sign In</button></p>
- </form>'''
- @app.route('/signin', methods=['POST'])
- def signin():
- # 需要从request对象读取表单内容:
- if request.form['username']=='admin' and request.form['password']=='password':
- return '<h3>Hello, admin!</h3>'
- return '<h3>Bad username or password.</h3>'
- if __name__ == '__main__':
- app.run()
- from flask import Flask, request, render_template
- app = Flask(__name__)
- @app.route('/', methods=['GET', 'POST'])
- def home():
- return render_template('home.html')
- @app.route('/signin', methods=['GET'])
- def signin_form():
- return render_template('form.html')
- @app.route('/signin', methods=['POST'])
- def signin():
- username = request.form['username']
- password = request.form['password']
- if username=='admin' and password=='password':
- return render_template('signin-ok.html', username=username)
- return render_template('form.html', message='Bad username or password', username=username)
- if __name__ == '__main__':
- app.run()
需要templates目录
Flask的更多相关文章
- flask+sqlite3+echarts2+ajax数据可视化
前提: 准备Python + Flask+Sqlite3的平台环境(windows系统) 前面一节介绍flask怎么安装了,剩下sqlite3下载后解压,然后环境变量添加解压路径就行了 附加下载地址: ...
- flask+sqlite3+echarts2+ajax数据可视化报错:UnicodeDecodeError: 'utf8' codec can't decode byte解决方法
flask+sqlite3+echarts2+ajax数据可视化报错: UnicodeDecodeError: 'utf8' codec can't decode byte 解决方法: 将 py文件和 ...
- Windows下快速安装Flask的一次经历
前提: 1.已安装python版本(一般都是2.X) 2.已安装easy_install python安装,记得配置Python的环境变量,例如:我的直接在Path上加 G:\Python 验证安装P ...
- 使用Flask设计带认证token的RESTful API接口[翻译]
上一篇文章, 使用python的Flask实现一个RESTful API服务器端 简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...
- 使用python的Flask实现一个RESTful API服务器端[翻译]
最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文将会使用python的Flask框架轻松实现一个RESTful的服务 ...
- python flask (一)
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World ...
- flask源码分析
本flask源码分析不间断更新 而且我分析的源码全是我个人觉得是很beautiful的 1 flask-login 1.1 flask.ext.login.login_required(func),下 ...
- Python flask 基于 Flask 提供 RESTful Web 服务
转载自 http://python.jobbole.com/87118/ 什么是 REST REST 全称是 Representational State Transfer,翻译成中文是『表现层状态转 ...
- Python flask @app.route
转载自 http://python.jobbole.com/80956/ 下面是Flask主页给我们的第一个例子,我们现在就由它入手,深入理解“@app.route()”是如何工作的. ...
- Flask 框架入门
Flask Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . 安装 Flask 依赖两个外部库, We ...
随机推荐
- js注册登录审核
<script type="text/javascript"> $(function(){ $("#sendSms").click(function ...
- JAVA与ABA问题
在<JAVA并发编程实战>的第15.4.4节中看到了一些关于ABA问题的描述.有一篇文章摘录了书里的内容. 书中有一段内容为: 如果在算法中采用自己的方式来管理节点对象的内存,那么可能出现 ...
- Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for details. ShareSDK 也有这种错误
Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for ...
- Python 开源异步并发框架的未来
http://segmentfault.com/a/1190000000471602 开源 Python 是开源的,介绍的这几个框架 Twisted.Tornado.Gevent 和 tulip 也都 ...
- 【leetcode】Trapping Rain Water(hard)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- SaaS系列介绍之八: SaaS的运营模式
1 引言 软件的核心是它为用户解决领域相关问题的能力. ________Eric Evans,<领域驱动设计> 传统的软件生命周期中,软件的维护占整个过程的70 ...
- [itint5]支持删除的后继查询
http://www.itint5.com/oj/#49 这一题一开始想到是用HashSet+链表来做,链表记录prev和next.这样也可以,后来看到都是连续的整数,而且交流了一下觉得可以用类似并查 ...
- [mock]12月27日
一开始介绍项目,最后的反馈是,还是说得不清楚,需要再准备准备. 然后两道题,第一题是有个数组,有2*n个数字,从1~n.比如n=3的数组,{1,2,2,3,1,3}.然后两两相同的数字删除,每次删除得 ...
- python string 连接test
def strTest(): name = "" for i in range(10): name += "hello" #print name def str ...
- Yii CActiveForm
http://blog.sina.com.cn/s/blog_685213e70101mo4i.html 文档: http://www.yiiframework.com/doc/api/1.1/CAc ...