werkzeug

Flask 的 Context 机制

被刻印的时光 ゝ 提交于 2020-01-04 02:08:21
转自https://blog.tonyseek.com/post/the-context-mechanism-of-flask/ Flask 的 Context 机制 2014 年 07 月 21 日 用过 Flask 做 Web 开发的同学应该不会不记得 App Context 和 Request Context 这两个名字——这两个 Context 算是 Flask 中比较特色的设计。 [1] 从一个 Flask App 读入配置并启动开始,就进入了 App Context,在其中我们可以访问配置文件、打开资源文件、通过路由规则反向构造 URL。 [2] 当一个请求进入开始被处理时,就进入了 Request Context,在其中我们可以访问请求携带的信息,比如 HTTP Method、表单域等。 [3] 所以,这两个 Context 也成了 Flask 框架复杂度比较集中的地方,对此有评价认为 Flask 的这种设计比 Django、Tornado 等框架的设计更为晦涩。 [4] 我不认同这种评价。对于一个 Web 应用来说,“应用” 和 “请求” 的两级上下文在理念上是现实存在的,如果理解了它们,那么使用 Flask 并不会晦涩;即使是使用 Django、Tornado,理解了它们的 Context 也非常有利于做比官网例子更多的事情(例如编写 Middleware)。

Flask入门

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

Can I call the werkzeug debugger in Django without needing to raise an exception or using assertions?

有些话、适合烂在心里 提交于 2020-01-02 17:57:20
问题 I'm currently using Werkzeug together with django-extensions and I'm able to call the werkzeug debugger by raising an exception or making a false assertion. Is it possible to just set a breakpoint for werkzeug like import pdb; pdb.set_trace() does? 回答1: If you are using Apache/mod_wsgi, all you need to do is modify your .wsgi file to include: from werkzeug.debug import DebuggedApplication and application = get_wsgi_application() application = DebuggedApplication(application, evalex=True) Then

Using CherryPy/Cherryd to launch multiple Flask instances

余生长醉 提交于 2020-01-01 02:34:11
问题 Per suggestions on SO/SF and other sites, I am using CherryPy as the WSGI server to launch multiple instances of a Python web server I built with Flask. Each instance runs on its own port and sits behind Nginx. I should note that the below does work for me, but I'm troubled that I have gone about things the wrong way and it works "by accident". Here is my current cherrypy.conf file: [global] server.socket_host = '0.0.0.0' server.socket_port = 8891 request.dispatch: cherrypy.dispatch

how to store binary file recieved by Flask into postgres

不羁的心 提交于 2019-12-30 07:17:24
问题 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

how to store binary file recieved by Flask into postgres

风格不统一 提交于 2019-12-30 07:17:08
问题 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

Download a file from a Flask-based Python server

前提是你 提交于 2019-12-30 06:38:09
问题 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()

Flask app that routes based on subdomain

时间秒杀一切 提交于 2019-12-29 18:02:39
问题 I want to have my top-level domain as a portal for various subdomains that correspond to different sections of my site. example.com should route to a welcome.html template. eggs.example.com should route to an "eggs" subsection or application of the site. How would I achieve this in Flask? 回答1: @app.route() takes a subdomain argument to specify what subdomain the route is matched on. Blueprint also takes a subdomain argument to set subdomain matching for all routes in a blueprint. You must set

Flask app that routes based on subdomain

偶尔善良 提交于 2019-12-29 18:00:28
问题 I want to have my top-level domain as a portal for various subdomains that correspond to different sections of my site. example.com should route to a welcome.html template. eggs.example.com should route to an "eggs" subsection or application of the site. How would I achieve this in Flask? 回答1: @app.route() takes a subdomain argument to specify what subdomain the route is matched on. Blueprint also takes a subdomain argument to set subdomain matching for all routes in a blueprint. You must set

Get IP address of visitors using Flask for Python

随声附和 提交于 2019-12-27 10:19:10
问题 I'm making a website where users can log on and download files, using the Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case). I need to get the IP address of users when they log on (for logging purposes). Does anyone know how to do this? Surely there is a way to do it with Python? 回答1: See the documentation on how to access the Request object and then get from this same Request object, the attribute remote_addr . Code example from flask import request from flask