werkzeug

What's the right approach for calling functions after a flask app is run?

扶醉桌前 提交于 2019-11-28 06:17:14
I'm a little confused about how to do something that I thought would be quite simple. I have a simple app written using Flask . It looks something like this: from flask import Flask app = Flask(__name__) def _run_on_start(a_string): print "doing something important with %s" % a_string @app.route('/') def root(): return 'hello world' if __name__ == "__main__": if len(sys.argv) < 2: raise Exception("Must provide domain for application execution.") else: DOM = sys.argv[1] _run_on_start("%s" % DOM) app.run(debug=True) What I'm finding is that my terminal is outputting the print statements in _run

Flask框架学习

不想你离开。 提交于 2019-11-28 02:46:05
快速入门 1 from flask import Flask 2 app = Flask(__name__) 3 4 @app.route('/') 5 def hello_world(): 6 return 'Hello World!' 7 8 if __name__ == '__main__': 9 app.run() 把它保存为 hello.py (或是类似的),然后用 Python 解释器来运行。 确保你的应用文件名不是 flask.py ,因为这将与 Flask 本身冲突。 1 $ python hello.py 2 * Running on http://127.0.0.1:5000/ 分析: 1. 首先,我们导入了 Flask 类。这个类的实例将会是我们的 WSGI 应用程序。 2. 接下来,我们创建一个该类的实例,第一个参数是应用模块或者包的名称。 如果你使用单一的模块(如本例),你应该使用 __name__ ,因为模块的名称将会因其作为单独应用启动还是作为模块导入而有不同( 也即是 '__main__' 或实际的导入名)。这是必须的,这样 Flask 才知道到哪去找模板、静态文件等等 3. 然后,我们使用 route() 装饰器告诉 Flask 什么样的URL 能触发我们的函数,URL和执行的视图函数(函数 index )的关系保存在app.url_map属性上

Changing request method using hidden field _method in Flask

若如初见. 提交于 2019-11-28 00:30:24
问题 Started picking up Python and Flask as a learning exercise, and coming from PHP/Symfony2, I could add a hidden _method field to a form to override the POST method with either a DELETE or PUT. It seems Flask doesn't support this natively, and I've been hacking around with various ideas including http://flask.pocoo.org/snippets/38/, which works, but involves putting the override in the form action, rather than as a hidden field, which IMO makes the URL look unsightly. There is a snippet in the

【Python Werkzeug】

橙三吉。 提交于 2019-11-27 18:51:14
原文: http://blog.gqylpy.com/gqy/350 "首先, Werkzeug 是一个 WSGI工具包 ,它可以作为一个Web框架的底层库。 需要注意的是,Werkzeug不是一个web服务器,也不是一个web框架,而是一个工具包,官方的介绍说是一个 WSGI 工具包,它可以作为一个 Web 框架的底层库,因为它封装好了很多 Web 框架的东西,例如 Request 、 Response 等等。 下面我们将使用Werkzeug来创建一个简单的web服务器,来说明Werkzeug怎么使用。 安装: pip install Werkzeug from werkzeug.wrappers import Response, Request from werkzeug.serving import run_simple @Request.application def app(request): print(request) # <Request 'http://127.0.0.1:8000/favicon.ico' [GET]> print(request.method) # 请求类型 print(request.path) # /favicon.ico return Response('Hello Werkzeug') run_simple(hostname='127

Flask werkzeug request.authorization is none but Authorization headers present

天大地大妈咪最大 提交于 2019-11-27 17:51:23
问题 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': ''

Rendering a python dict in Jinja2 / Werkzeug

六月ゝ 毕业季﹏ 提交于 2019-11-27 15:42:10
问题 I'm playing with a url shortener (basing it on the Shortly demo app from Werkzeug). I have a dict like this - ('1', {'target': 'http://10.58.48.103:5000/', 'clicks': '1'}) ('3', {'target': 'http://slash.org', 'clicks': '4'}) ('2', {'target': 'http://10.58.48.58:5000/', 'clicks': '1'}) ('5', {'target': 'http://de.com/a', 'clicks': '0'}) which is returned in url_list and used by render_template def on_list_urls(self, request): url_list = self.get_urls() return self.render_template('list_urls

Flask/Werkzeug how to attach HTTP content-length header to file download

↘锁芯ラ 提交于 2019-11-27 13:48:51
问题 I am using Flask (based on Werkzeug) which uses Python. The user can download a file, I'm using the send_from_directory-function. However when actually downloading the file, the HTTP header content-length is not set. So the user has no idea how big the file being downloaded is. I can use os.path.getsize(FILE_LOCATION) in Python to get the file size (in bytes), but cannot find a way to set the content-length header in Flask. Any ideas? 回答1: I believe you'd do something like this (untested):

RuntimeError: working outside of application context

做~自己de王妃 提交于 2019-11-27 13:27:18
app.py from flask import Flask, render_template, request,jsonify,json,g import mysql.connector app = Flask(__name__) **class TestMySQL():** @app.before_request def before_request(): try: g.db = mysql.connector.connect(user='root', password='root', database='mysql') except mysql.connector.errors.Error as err: resp = jsonify({'status': 500, 'error': "Error:{}".format(err)}) resp.status_code = 500 return resp @app.route('/') def input_info(self): try: cursor = g.db.cursor() cursor.execute ('CREATE TABLE IF NOT EXISTS testmysql (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40) NOT NULL

How to apply integration tests (rather than unit tests) to a Flask RESTful API

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 12:17:34
问题 [As per https://stackoverflow.com/a/46369945/1021819, the title should refer to integration tests rather than unit tests] Suppose I'd like to test the following Flask API (from here): import flask import flask_restful app = flask.Flask(__name__) api = flask_restful.Api(app) class HelloWorld(flask_restful.Resource): def get(self): return {'hello': 'world'} api.add_resource(HelloWorld, '/') if __name__ == "__main__": app.run(debug=True) Having saved this as flaskapi.py and run it, in the same

Flask源码剖析详解

纵然是瞬间 提交于 2019-11-27 10:46:02
1. 前言 本文将基于 flask 0.1 版本(git checkout 8605cc3)来分析flask的实现,试图理清flask中的一些概念,加深读者对flask的理解,提高对flask的认识。从而,在使用flask过程中,能够减少困惑,胸有成竹,遇bug而不惊。 在试图理解flask的设计之前,你知道应该知道以下几个概念: flask(web框架)是什么 WSGI是什么 jinja2是什么 Werkzeug是什么 本文将首先回答这些问题,然后再分析flask源码。 2. 知识准备 2.1 WSGI 下面这张图来自 这里 ,通过这张图,读者对web框架所处的位置和WSGI协议能够有一个感性的认识。 WSGI wikipedia 上对WSGI的解释就比较通俗易懂。为了更好的理解WSGI,我们来看一个 例子 : from eventlet import wsgi import eventlet def hello_world(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello, World!\r\n'] wsgi.server(eventlet.listen(('', 8090)), hello_world) 我们定义了一个hello