1. #environ:一个包含所有HTTP请求信息的dict对象
  2. #start_response:一个发送HTTP响应的函数
  3. def application(environ, start_response):
  4. method = environ['REQUEST_METHOD']
  5. path = environ['PATH_INFO']
  6.  
  7. #if method=='GET' and path=='/':
  8. # return handle_home(environ, start_response)
  9.  
  10. start_response('200 OK', [('Content-Type', 'text/html')])
  11. #[1:] 切片 第2个字符到后面所有字符
  12. body = '<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')
  13. return [body.encode('utf-8')]
  14. #return [b'<h1>Hello, web!</h1>']
  15.  
  16. # 从wsgiref模块导入:
  17. from wsgiref.simple_server import make_server
  18.  
  19. # 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
  20. httpd = make_server('', 8000, application)
  21. print('Serving HTTP on port 8000...')
  22. # 开始监听HTTP请求:
  23. httpd.serve_forever()
  1. from flask import Flask
  2. from flask import request
  3.  
  4. app = Flask(__name__)
  5.  
  6. @app.route('/', methods=['GET', 'POST'])
  7. def home():
  8. return '<h1>Home</h1>'
  9.  
  10. @app.route('/signin', methods=['GET'])
  11. def signin_form():
  12. return '''<form action="/signin" method="post">
  13. <p><input name="username"></p>
  14. <p><input name="password" type="password"></p>
  15. <p><button type="submit">Sign In</button></p>
  16. </form>'''
  17.  
  18. @app.route('/signin', methods=['POST'])
  19. def signin():
  20. # 需要从request对象读取表单内容:
  21. if request.form['username']=='admin' and request.form['password']=='password':
  22. return '<h3>Hello, admin!</h3>'
  23. return '<h3>Bad username or password.</h3>'
  24.  
  25. if __name__ == '__main__':
  26. app.run()
  1. from flask import Flask, request, render_template
  2.  
  3. app = Flask(__name__)
  4.  
  5. @app.route('/', methods=['GET', 'POST'])
  6. def home():
  7. return render_template('home.html')
  8.  
  9. @app.route('/signin', methods=['GET'])
  10. def signin_form():
  11. return render_template('form.html')
  12.  
  13. @app.route('/signin', methods=['POST'])
  14. def signin():
  15. username = request.form['username']
  16. password = request.form['password']
  17. if username=='admin' and password=='password':
  18. return render_template('signin-ok.html', username=username)
  19. return render_template('form.html', message='Bad username or password', username=username)
  20.  
  21. if __name__ == '__main__':
  22. app.run()

需要templates目录

Flask的更多相关文章

  1. flask+sqlite3+echarts2+ajax数据可视化

    前提: 准备Python + Flask+Sqlite3的平台环境(windows系统) 前面一节介绍flask怎么安装了,剩下sqlite3下载后解压,然后环境变量添加解压路径就行了 附加下载地址: ...

  2. flask+sqlite3+echarts2+ajax数据可视化报错:UnicodeDecodeError: 'utf8' codec can't decode byte解决方法

    flask+sqlite3+echarts2+ajax数据可视化报错: UnicodeDecodeError: 'utf8' codec can't decode byte 解决方法: 将 py文件和 ...

  3. Windows下快速安装Flask的一次经历

    前提: 1.已安装python版本(一般都是2.X) 2.已安装easy_install python安装,记得配置Python的环境变量,例如:我的直接在Path上加 G:\Python 验证安装P ...

  4. 使用Flask设计带认证token的RESTful API接口[翻译]

    上一篇文章, 使用python的Flask实现一个RESTful API服务器端  简单地演示了Flask实的现的api服务器,里面提到了因为无状态的原则,没有session cookies,如果访问 ...

  5. 使用python的Flask实现一个RESTful API服务器端[翻译]

    最近这些年,REST已经成为web services和APIs的标准架构,很多APP的架构基本上是使用RESTful的形式了. 本文将会使用python的Flask框架轻松实现一个RESTful的服务 ...

  6. python flask (一)

    from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World ...

  7. flask源码分析

    本flask源码分析不间断更新 而且我分析的源码全是我个人觉得是很beautiful的 1 flask-login 1.1 flask.ext.login.login_required(func),下 ...

  8. Python flask 基于 Flask 提供 RESTful Web 服务

    转载自 http://python.jobbole.com/87118/ 什么是 REST REST 全称是 Representational State Transfer,翻译成中文是『表现层状态转 ...

  9. Python flask @app.route

    转载自 http://python.jobbole.com/80956/ 下面是Flask主页给我们的第一个例子,我们现在就由它入手,深入理解“@app.route()”是如何工作的.         ...

  10. Flask 框架入门

    Flask Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . 安装 Flask 依赖两个外部库, We ...

随机推荐

  1. js注册登录审核

    <script type="text/javascript"> $(function(){ $("#sendSms").click(function ...

  2. JAVA与ABA问题

    在<JAVA并发编程实战>的第15.4.4节中看到了一些关于ABA问题的描述.有一篇文章摘录了书里的内容. 书中有一段内容为: 如果在算法中采用自己的方式来管理节点对象的内存,那么可能出现 ...

  3. 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 ...

  4. Python 开源异步并发框架的未来

    http://segmentfault.com/a/1190000000471602 开源 Python 是开源的,介绍的这几个框架 Twisted.Tornado.Gevent 和 tulip 也都 ...

  5. 【leetcode】Trapping Rain Water(hard)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. SaaS系列介绍之八: SaaS的运营模式

    1 引言 软件的核心是它为用户解决领域相关问题的能力.               ________Eric Evans,<领域驱动设计> 传统的软件生命周期中,软件的维护占整个过程的70 ...

  7. [itint5]支持删除的后继查询

    http://www.itint5.com/oj/#49 这一题一开始想到是用HashSet+链表来做,链表记录prev和next.这样也可以,后来看到都是连续的整数,而且交流了一下觉得可以用类似并查 ...

  8. [mock]12月27日

    一开始介绍项目,最后的反馈是,还是说得不清楚,需要再准备准备. 然后两道题,第一题是有个数组,有2*n个数字,从1~n.比如n=3的数组,{1,2,2,3,1,3}.然后两两相同的数字删除,每次删除得 ...

  9. python string 连接test

    def strTest(): name = "" for i in range(10): name += "hello" #print name def str ...

  10. Yii CActiveForm

    http://blog.sina.com.cn/s/blog_685213e70101mo4i.html 文档: http://www.yiiframework.com/doc/api/1.1/CAc ...