werkzeug

12.1 flask基础之简单实用

余生长醉 提交于 2019-11-29 14:38:06
一、Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后触发Flask框架,开发人员基于Flask框架提供的功能对请求进行相应的处理,并返回给用户,如果要返回给用户复杂的内容时,需要借助jinja2模板来实现对模板的处理,即:将模板和数据进行渲染,将渲染后的字符串返回给用户浏览器。 “微”(micro) 并不表示你需要把整个 Web 应用塞进单个 Python 文件(虽然确实可以 ),也不意味着 Flask 在功能上有所欠缺。微框架中的“微”意味着 Flask 旨在保持核心简单而易于扩展。Flask 不会替你做出太多决策——比如使用何种数据库。而那些 Flask 所选择的——比如使用何种模板引擎——则很容易替换。除此之外的一切都由可由你掌握。如此,Flask 可以与您珠联璧合。 默认情况下,Flask 不包含数据库抽象层、表单验证,或是其它任何已有多种库可以胜任的功能。然而,Flask 支持用扩展来给应用添加这些功能,如同是 Flask 本身实现的一样。众多的扩展提供了数据库集成、表单验证、上传处理、各种各样的开放认证技术等功能。Flask 也许是“微小”的

Flask入门:介绍与简单使用

好久不见. 提交于 2019-11-29 14:37:10
1、Flask简介   Flask是一个使用 Python 编写的轻量级 Web 应用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 。Flask使用 BSD 授权。   Flask也被称为 “microframework” ,因为它使用简单的核心,用 extension 增加其他功能。Flask没有默认使用的数据库、窗体验证工具。 2、Flask的依赖模块   当前Flask的版本为1.0.2。   跳转到flask模块,使用pipreqs获取flask的依赖: pipreqs ./ --encoding=utf-8   生成的requirements.txt内容如下: blinker==1.4 Jinja2==2.10 Click==7.0 itsdangerous==1.1.0 Werkzeug==0.14.1 Flask==1.0.2 setuptools==39.0.1 pyOpenSSL==18.0.0 python-dotenv==0.10.0 3、Flask依赖模块介绍 3.1、Blinker    官方文档: Blinker Documentation    Blinker的使用: http://python.jobbole.com/85554/   Blinker 是一个基于Python的强大的信号库

Python 【web框架】之Flask

我怕爱的太早我们不能终老 提交于 2019-11-29 14:36:52
flask 是Python实现的轻量级web框架。没有表单,orm等,但扩展性很好。很多Python web开发者十分喜欢。本篇介绍flask的简单使用及其扩展。 文中示例源码已经传到github: https://github.com/ZingP/webstudy.git . 1 安装flask pip install flask 2 框架本质 flask是基于Werkzeug模块和Jinja2模板引擎实现的。前者实现了WSGI协议。我们来看一下用Werkzeug怎么实现一个web服务端: from werkzeug.wrappers import Request, Response @Request.application def hello(request): print(request) return Response('Hello Werkzeug!') if __name__ == '__main__': from werkzeug.serving import run_simple run_simple('localhost', 9000, hello)   然后运行该文件,从浏览器上访问http://localhost:9000/ 就能看到Hello Werkzeug!了。 3 快速开始 开始第一个flask: from flask import Flask

Get IP Address when testing flask application through nosetests

做~自己de王妃 提交于 2019-11-29 11:21:19
问题 My application depends on request.remote_addr which is None when i run tests through nosetests which uses app.test_client().post('/users/login', ....) . How can I emulate an IP (127.0.0.1 works fine) when I run tests? I've tried setting environment variables, sent in headers with the post() method and I've digged through nosetests, werkzeugs and flasks documentation but nothing I've tried has worked. 回答1: You can set options for the underlying Werkzeug environment using environ_base: from

Server is serving old versions of static files, but trimmed or padded to match length of new versions

你。 提交于 2019-11-29 06:07:16
The symptoms of my problem match this question pretty much exactly: Changed static files are cropped/padded to the new size and served the old - Fedora Whenever I make changes to my static files (e.g. .js and .css), those changes don't show up in the served file. However, if my changes cause the file to change length, then the served file does match the new length: If I delete characters from anywhere in the static file, then the served file is trimmed at the end by that many characters. If I add characters to anywhere in the static file, then the served file is padded with that many � (that's

Build error with variables and url_for in Flask

怎甘沉沦 提交于 2019-11-29 05:36:35
Have found one or two people on the interwebs with similar problems, but haven't seen a solution posted anywhere. I'm getting a build error from the code/template below, but can't figure out where the issue is or why it's occurring. It appears that the template isn't recognizing the function, but don't know why this would be occurring. Any help would be greatly appreciated - have been pounding my against the keyboard for two nights now. Function: @app.route('/viewproj/<proj>', methods=['GET','POST']) def viewproj(proj): ... Template Excerpt: {% for project in projects %} <li> <a href="{{ url

Flask/Werkzeug, how to return previous page after login

三世轮回 提交于 2019-11-29 05:33:57
问题 I am using the Flask micro-framework which is based on Werkzeug, which uses Python. Before each restricted page there is a decorator to ensure the user is logged in, currently returning them to the login page if they are not logged in, like so: # Decorator def logged_in(f): @wraps(f) def decorated_function(*args, **kwargs): try: if not session['logged_in']: flash('Please log in first...', 'error') return redirect(url_for('login')) else: return f(*args, **kwargs) except KeyError: flash('Please

Flask werkzeug request.authorization is none but Authorization headers present

霸气de小男生 提交于 2019-11-29 03:45:29
I am POSTing some JSON data and adding an Authorization header. However, the request object does not have the correct authorization property. HTTP_AUTHORIZATION and headers both show the proper authorization details. {'authorization': None, 'cookies': {}, 'environ': {'CONTENT_LENGTH': '81', 'CONTENT_TYPE': u'application/json', 'HTTP_AUTHORIZATION': 'testkey:', 'HTTP_CONTENT_LENGTH': '81', 'HTTP_CONTENT_TYPE': 'application/json', 'HTTP_HOST': 'test', 'PATH_INFO': '/v1/test', 'QUERY_STRING': '', 'REQUEST_METHOD': 'POST', 'SCRIPT_NAME': '', 'SERVER_NAME': 'test', 'SERVER_PORT': '80', 'SERVER

X-Forwarded-Proto and Flask

你。 提交于 2019-11-29 03:13:11
I have precisely the same problem described in this SO question and answer . The answer to that question is a nice work around but I don't understand the fundamental problem. Terminating SSL at the load balancer and using HTTP between the load balancer and web/app servers is very common. What piece of the stack is not respecting the X-Forwarded-Proto? Is it werkzeug? Flask? uwsgi? In my case I'm using an AWS ELB (which sets X-Forwarded-Proto) => Nginx (which forwards along X-Forwarded-Proto to uwsgi). But in the python app I have to subclass Flask Request as described in the question I

教你如何在Ubuntu 18.04 下安装 Tensorflow(CPU)

六眼飞鱼酱① 提交于 2019-11-29 02:40:22
最近我开始学习深度学习框架Tensorflow,一开始在Windows平台下的anaconda下安装,由于anaconda安装几次后navigator打开老是出现闪退的问题,所以决定换个Ubuntu下继续折腾tensorflow。本人笔记本没有NVIDIA显卡,只装的CPU版本的。而且是在虚拟机下的,下面开始吧。 先安装好Ubuntu 18.04版本的系统(最好是Ubuntu的14.04版本以上),Ubuntu系统已经有了了Python 3.7.7.7,所以不需要再安装Python了。 一、首先更新源为阿里云软件源,增加下载速度 (1)备份当前也就是默认官方的源列表 sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup (2)删除sources.list文件中的列表,删除全部内容 sudo gedit /etc/apt/sources.list1 (3)替换内容后,保存 deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http:/