werkzeug

Flask

匿名 (未验证) 提交于 2019-12-02 23:49:02
一、Flask了解 #flask是一个基于python开发并且依赖jinja2和Werkzeug服务的微型框架 对于werkzeug本质是socket服务端,其用于接收http请求并对请求进行预处理,然后触发flask框架,开发人员基于flask框架提供的功能对请求进行相应的处理,并返回给客户,如果返回给用户复杂的内容时,需要借助jinja2模板来实现对模板的处理,即:将模板和数据进行渲染,将渲染后的字符串返回给用户的浏览器 “微(micro)”并不代表你需要把整个web应用塞进单个python文件(虽然确实可以),也不意味着flask在功能上有所欠缺,微框架中的“微"意味着flask旨在保持核心简单而易于扩展,Flask 不会替你做出太多决策――比如使用何种数据库。而那些 Flask 所选择的――比如使用何种模板引擎――则很容易替换。除此之外的一切都由可由你掌握。如此,Flask 可以与您珠联璧合。 默认情况下,Flask 不包含数据库抽象层、表单验证,或是其它任何已有多种库可以胜任的功能。然而,Flask 支持用扩展来给应用添加这些功能,如同是 Flask 本身实现的一样。众多的扩展提供了数据库集成、表单验证、上传处理、各种各样的开放认证技术等功能。Flask 也许是“微小”的,但它已准备好在需求繁杂的生产环境中投入使用 View Code 1、安装 pip3 install

Saving uploaded base64 data gives TypeError: a bytes-like object is required, not 'str'

泪湿孤枕 提交于 2019-12-02 21:22:59
问题 I am using a JavaScript library to upload an image. It places the image data in a form field that is a JSON encoded object with the file name and base64 encoded data. When I try to save the image in Flask, I get this error: builtins.TypeError TypeError: a bytes-like object is required, not 'str' The image file is created with the correct name, but the data is corrupt, I can't open it. How do I save the uploaded data? app = Flask(__name__) UPLOAD_FOLDER = app.root_path + '/images/' @app.route(

Saving uploaded base64 data gives TypeError: a bytes-like object is required, not 'str'

核能气质少年 提交于 2019-12-02 08:49:56
I am using a JavaScript library to upload an image. It places the image data in a form field that is a JSON encoded object with the file name and base64 encoded data. When I try to save the image in Flask, I get this error: builtins.TypeError TypeError: a bytes-like object is required, not 'str' The image file is created with the correct name, but the data is corrupt, I can't open it. How do I save the uploaded data? app = Flask(__name__) UPLOAD_FOLDER = app.root_path + '/images/' @app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': image_base = json.loads(request

一 .Flask使用

寵の児 提交于 2019-12-02 03:16:56
一 .Flask初步使用 1.Flask简介 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后触发Flask框架,开发人员基于Flask框架提供的功能对请求进行相应的处理,并返回给用户,如果要返回给用户复杂的内容时,需要借助jinja2模板来实现对模板的处理,即:将模板和数据进行渲染,将渲染后的字符串返回给用户浏览器。 werkzeug简介 Werkzeug是一个WSGI工具包,他可以作为一个Web框架的底层库。这里稍微说一下, werkzeug 不是一个web服务器,也不是一个web框架,而是一个工具包,官方的介绍说是一个 WSGI 工具包,它可以作为一个 Web 框架的底层库,因为它封装好了很多 Web 框架的东西,例如 Request,Response 等等 flask 特点 1. 短小精悍 可扩展性强 的一个web框架 2. 上下文管理机制 3. 依赖wsgi :werkzurg 2. werkzurg学习(注意下载flask就带这个模块 了解) 示例1:from werkzeug.wrappers import Request, Response from werkzeug.serving import run

Flask route giving 404 with floating point numbers in the URL

只谈情不闲聊 提交于 2019-12-01 15:30:33
I have the following route definition in my Flask app's server.py: @app.route('/nearby/<float:lat>/<float:long>') def nearby(lat, long): for truck in db.trucks.find({'loc': {'$near': [lat, long]}}).limit(5): if truck.has_key('loc'): del truck['loc'] return json.dumps(trucks) But when I go to http://localhost:5000/nearby/37.7909470419234/-122.398633589404 , I get a 404. The other routes work fine, so it's an issue with this one. What am I doing wrong here? The built-in FloatConverter does not handle negative numbers. Write a custom converter to handle negatives. This converter also treats

Flask route giving 404 with floating point numbers in the URL

吃可爱长大的小学妹 提交于 2019-12-01 15:13:01
问题 I have the following route definition in my Flask app's server.py: @app.route('/nearby/<float:lat>/<float:long>') def nearby(lat, long): for truck in db.trucks.find({'loc': {'$near': [lat, long]}}).limit(5): if truck.has_key('loc'): del truck['loc'] return json.dumps(trucks) But when I go to http://localhost:5000/nearby/37.7909470419234/-122.398633589404 , I get a 404. The other routes work fine, so it's an issue with this one. What am I doing wrong here? 回答1: The built-in FloatConverter does

01-视图及路由

こ雲淡風輕ζ 提交于 2019-12-01 12:19:20
1、Flask简介   Web应用程序的本质,Web(World Wide Web)诞生最初的目的,是为了利用互联网交流工作文档。 Web框架   Web框架就是协助开发者快速开发Web应用程序的一套功能代码。开发者只需要按照框架约定要求,在指定位置写上自己的业务逻辑代码。(就是要么重新造轮子,要么用轮子造轮子) 为什么要用Web框架?   Web网站发展至今,特别是服务端,涉及到的知识、内容非常广泛。这对程序员的要求也会越来越高。如果采用成熟,稳健的框架,那么一些基础的工作,比如,安全性,数据流控制等都可以用框架来处理,那么程序开发人员可以把精力放在具体任务的逻辑上面。使用框架的优点(1、稳定性和可拓展性强2、可以降低开发难度,提高开发效率)。   python中常用的Web框架有flask,django,tornado Flask   Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架。   Flask 本身相当于一个内核,其他几乎所有的功能都要用到扩展(邮件扩展Flask-Mail,用户认证Flask-Login,数据库Flask-SQLAlchemy),都需要用第三方的扩展来实现。比如可以用 Flask 扩展加入ORM、窗体验证工具,文件上传、身份验证等。Flask

nginx + uwsgi + flask - disabling custom error pages

谁说我不能喝 提交于 2019-12-01 03:25:38
Is it possible to disable nginx's custom error pages - if I may call them that - to display my framework's exception pages? I can't really see my werkzeug debugger tool rendered in html... UPDATE OK, I got to make a very very simple flask application to work and I'll post the bits: /home/my_user/.virtualenvs/nginx-test/etc/nginx.conf worker_processes 1; events { worker_connections 1024; } http { server { listen 5000; server_name localhost; access_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/access.log; error_log /home/my_user/.virtualenvs/nginx-test/lib/nginx/error.log; location / {

how to store binary file recieved by Flask into postgres

半城伤御伤魂 提交于 2019-12-01 00:44:53
I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route('/upload', methods=['POST']) def upload_file(): def allowed_file(f): return True file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(upload_dir(), filename)) return "", 200 I would like to store it in a BYTEA column in postgres, I am not sure how to bind the "data" argument to the insert statement db.session.execute(""" INSERT INTO uploaded_file(id, name, type, data) VALUES (:id, :name, :type

Download a file from a Flask-based Python server

寵の児 提交于 2019-11-30 20:31:28
I'm trying to make work a code that I found at this URL: http://code.runnable.com/UiIdhKohv5JQAAB6/how-to-download-a-file-generated-on-the-fly-in-flask-for-python My goal is to be able to download a file on a web browser when the user access to a web service on my Flask-base Python server. So I wrote the following code: @app.route("/api/downloadlogfile/<path>") def DownloadLogFile (path = None): if path is None: self.Error(400) try: with open(path, 'r') as f: response = make_response(f.read()) response.headers["Content-Disposition"] = "attachment; filename=%s" % path.split("/")[2] return