werkzeug

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

寵の児 提交于 2019-11-28 21:24:43
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? I believe you'd do something like this (untested): from flask import Response response = Response() response.headers.add('content-length', str(os.path.getsize

Flask url_for generating http URL instead of https

邮差的信 提交于 2019-11-28 18:36:10
I am using url_for to generate a redirect URL when a user has logged out: return redirect(url_for('.index', _external=True)) However, when I changed the page to a https connection, the url_for still gives me http . I would like to explicitly ask url_for to add https at the beginning of a URL. Can you point me how to change it? I looked at Flask docs, without luck. With Flask 0.10, there will be a much better solution available than wrapping url_for . If you look at https://github.com/mitsuhiko/flask/commit/b5069d07a24a3c3a54fb056aa6f4076a0e7088c7 , a _scheme parameter has been added. Which

【Python Werkzeug】

ε祈祈猫儿з 提交于 2019-11-28 17:41:42
原文: 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.0

How to set up autoreload with Flask+uWSGI?

白昼怎懂夜的黑 提交于 2019-11-28 17:31:44
I am looking for something like uWSGI + django autoreload mode for Flask. You could try using supervisord as a manager for your Uwsgi app. It also has a watch function that auto-reloads a process when a file or folder has been "touched"/modified. You will find a nice tutorial here: Flask+NginX+Uwsgi+Supervisord I am running uwsgi version 1.9.5 and the option uwsgi --py-autoreload 1 works great If you're configuring uwsgi with command arguments, pass --py-autoreload=1 : uwsgi --py-autoreload=1 If you're using a .ini file to configure uwsgi and using uwsgi --ini , add the following to your .ini

Run python script as daemon at boot time (Ubuntu)

梦想与她 提交于 2019-11-28 17:20:21
I've created small web server using werkzeug and I'm able to run it in usual python way with python my_server.py . Pages load, everything works fine. Now I want to start it when my pc boots. What's the easiest way to do that? I've been struggling with upstart but it doesn't seem to "live in a background" cuz after I execute start my_server I immediately receive kernel: [ 8799.793942] init: my_server main process (7274) terminated with status 1 my_server.py: ... if __name__ == '__main__': from werkzeug.serving import run_simple app = create_app() run_simple('0.0.0.0', 4000, app) upstart

How do I use url_for if my method has multiple route annotations?

怎甘沉沦 提交于 2019-11-28 15:59:10
So I have a method that is accessible by multiple routes: @app.route("/canonical/path/") @app.route("/alternate/path/") def foo(): return "hi!" Now, how can I call url_for("foo") and know that I will get the first route? Nemoden Ok. It took some delving into the werkzeug.routing and flask.helpers.url_for code, but I've figured out. You just change the endpoint for the route (in other words, you name your route) @app.route("/canonical/path/", endpoint="foo-canonical") @app.route("/alternate/path/") def foo(): return "hi!" @app.route("/wheee") def bar(): return "canonical path is %s, alternative

Flask and Werkzeug: Testing a post request with custom headers

烂漫一生 提交于 2019-11-28 11:52:38
I'm currently testing my app with suggestions from http://flask.pocoo.org/docs/testing/ , but I would like to add a header to a post request. My request is currently: self.app.post('/v0/scenes/test/foo', data=dict(image=(StringIO('fake image'), 'image.png'))) but I would like to add a content-md5 to the request. Is this possible? My investigations: Flask Client (in flask/testing.py) extends Werkzeug's Client, documented here: http://werkzeug.pocoo.org/docs/test/ As you can see, post uses open . But open only has: Parameters: as_tuple – Returns a tuple in the form (environ, result) buffered –

Retrieving the url anchor in a werkzeug request

我们两清 提交于 2019-11-28 11:12:14
I have a DAV protocol that stores out-of-band data in the url anchor, e.g. the ghi in DELETE /abc.def#ghi . The server is a Flask application. I can see the request come in on the wire via tcpdump , but when I look at the werkzeug Request object (such as url() or base_url()), all I get back is /abc.def . The #ghi has been stripped out. Is there a method that returns this information, or do I have to subclass Request to handle this myself? If so, is there an example I can use as an inspiration? From Wikipedia ( Fragment Identifier ) (don't have the time to find it in the RFC): The fragment

【Python Werkzeug】

时间秒杀一切 提交于 2019-11-28 10:18:37
原文: 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.0

Capture a list of integers with a Flask route

淺唱寂寞╮ 提交于 2019-11-28 09:10:16
问题 I'm trying to implement a basic calculator in Flask. I define two url parameters, which is manageable when I only want to add two values. However, I want to add any number of values. How can I get a list of integers without writing an infinitely long route? @app.route('/add/<int:n1>,<int:n2>') def add(n1,n2): sum = n1+n2 return "%d" % (sum) I tried to solve my problem with this code, but it's not working integer_list = [] @app.route('/add/integer_list') def fun (integer_list): sum = 0 for