werkzeug

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

别来无恙 提交于 2019-11-27 09:28:54
问题 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? 回答1: 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/

2,flask入门(URL)

丶灬走出姿态 提交于 2019-11-27 08:42:13
video5 flask特点: 1,为框架,简介,高扩展性。 2,flask相关依赖(jinja2,werkzeug)设计优秀。 3,开发高效,如SQL的ORM video6 debug模式 我只推荐run->edit video7 配置文件 1,文件方式 若文件路劲不带,可加silent让它在找不到路径时候也不报错。 2,常规方式 video8 URL中的两种传参 path可以接受‘/’。 可以接受多个‘/’的URL: 可以接受UUID: 全宇宙都唯一的随机数,可以做主键。 any:可以指定多种路径 1 @app.route('/<any(blog,user):url_path>/<id>') 2 def detail(url_path,id): 3 if url_path == 'blog': 4 return 'Blog:%s' %id 5 else: 6 return 'User:%s' %id 作用:不同的URL对应相同的视图函数就这么做。 video9 url_for 带参数的list函数中的page代入url_for的过程。 如果多带一个count呢? 则将以查询字符串的形式出现,结果为: 带参数的视图函数有个功能: 当你浏览一个网页想要评论的时候发现自己没登陆,等你登陆后又能回到你刚才要评论的页面,这种情况下可以实现通过url_for带参数的视图函数。

104, 'Connection reset by peer' socket error, or When does closing a socket result in a RST rather than FIN?

别来无恙 提交于 2019-11-27 06:49:09
We're developing a Python web service and a client web site in parallel. When we make an HTTP request from the client to the service, one call consistently raises a socket.error in socket.py, in read: (104, 'Connection reset by peer') When I listen in with wireshark, the "good" and "bad" responses look very similar: Because of the size of the OAuth header, the request is split into two packets. The service responds to both with ACK The service sends the response, one packet per header (HTTP/1.0 200 OK, then the Date header, etc.). The client responds to each with ACK. (Good request) the server

Flask and Werkzeug: Testing a post request with custom headers

荒凉一梦 提交于 2019-11-27 06:33:37
问题 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

Werkzeug库——wrappers模块

孤街醉人 提交于 2019-11-27 04:44:12
Werkzeug库中的 wrappers 模块主要对 request 和 response 进行封装。 request 包含了客户端发往服务器的所有请求信息, response 包含了web应用返回给客户端的所有信息。 wrappers 模块对请求和响应的封装简化了客户端、服务器和web应用通信的流程。本文主要介绍 wrappers 模块中重要的类。 BaseRequest BaseRequest 是一个非常基础的请求类,它可以和其他的“混合”类结合在一起构建复杂的请求类。只要传递一个环境变量 environ (由 WSGI 服务器根据请求产生),便可以构造一个 BaseRequest 实例。其构造函数如下: 1 2 3 4 5 def __init__ ( self , environ , populate_request = True , shallow = False ) : self . environ = environ if populate_request and not shallow : self . environ [ 'werkzeug.request' ] = self self . shallow = shallow 初始化后,形成的实例 request 便具有了一些属性可以访问,这些属性只能以“只读”的方式访问。例如: url_charset want

Capture arbitrary path in Flask route

谁说我不能喝 提交于 2019-11-26 22:23:25
I have a simple Flask route that I want to capture a path to a file. If I use <path> in the rule, it works for /get_dir/one but not /get_dir/one/two . How can I capture an arbitrary path, so that path='/one/two/etc will be passed to the view function? @app.route('/get_dir/<path>') def get_dir(path): return path moodh Use the path converter to capture arbitrary length paths: <path:path> will capture a path and pass it to the path argument. The default converter captures a single string but stops at slashes, which is why your first url matched but the second didn't. If you also want to match the

URL routing conflicts for static files in Flask dev server

感情迁移 提交于 2019-11-26 20:02:47
问题 I want to define a url rule with three variable components, like: @app.route('/<var_1>/<var_2>/<var3>/') But I find that the development server evaluates such rules before trying to match for static files. So anything like: /static/images/img.jpg will be caught by my url rule, rather than being forwarded to the built-in static file handler. Is there a way to force the development server to match for static files first? P.S. This is only an issue if the rule has more than two variable

Flask reloader crashes with “no module named Scripts\\flask” on Windows

 ̄綄美尐妖づ 提交于 2019-11-26 19:07:43
When I run flask run on Windows, I get the following error C:\Python37\python.exe: No module named C:\Python37\Scripts\flask This was working previously, the issue started after I created a new env recently. This is a bug introduced in Werkzeug 0.15.5. Until 0.15.6 is released, you can run with python -m flask run instead to work around it. 来源: https://stackoverflow.com/questions/57114348/flask-reloader-crashes-with-no-module-named-scripts-flask-on-windows

Get raw POST body in Python Flask regardless of Content-Type header

二次信任 提交于 2019-11-26 18:14:25
Previously, I asked How to get data received in Flask request because request.data was empty. The answer explained that request.data is the raw post body, but will be empty if form data is parsed. How can I get the raw post body unconditionally? @app.route('/', methods=['POST']) def parse_request(): data = request.data # empty in some cases # always need raw data here, not parsed form data miracle2k Use request.get_data() to get the raw data, regardless of content type. The data is cached and you can subsequently access request.data , request.json , request.form at will. If you access request

RuntimeError: working outside of application context

余生颓废 提交于 2019-11-26 16:21:37
问题 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 (