werkzeug
Handling large file uploads with Flask
I think the super simple way to get around that simply sends the file in lots of small parts/chunks. So there are going to be two parts to making this work, the front-end (website) and backend (server). For the front-end part, you can use something like Dropzone.js which has no additional dependencies and decent CSS … Read more
X-Forwarded-Proto and Flask
You are missing the ProxyFix() middleware component. See the Flask Proxy Setups documentation. There is no need to subclass anything; simply add this middleware component to your WSGI stack: # Werkzeug 0.15 and newer from werkzeug.middleware.proxy_fix import ProxyFix from flask import Flask app = Flask(__name__) app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1) If you have Flask installed, you … Read more
104, ‘Connection reset by peer’ socket error, or When does closing a socket result in a RST rather than FIN?
I’ve had this problem. See The Python “Connection Reset By Peer” Problem. You have (most likely) run afoul of small timing issues based on the Python Global Interpreter Lock. You can (sometimes) correct this with a time.sleep(0.01) placed strategically. “Where?” you ask. Beats me. The idea is to provide some better thread concurrency in and … Read more
Flask url_for generating http URL instead of https
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 means you can do the following: url_for(‘secure_thingy’, _external=True, _scheme=”https”, viewarg1=1, …) _scheme sets the URL scheme, generating a URL like https://.. instead of http://. However, by default Flask only … Read more
RuntimeError: working outside of application context
Flask has an Application Context, and it seems like you’ll need to do something like: def test_connection(self): with app.app_context(): #test code You can probably also shove the app.app_context() call into a test setup method as well. Hope this helps.
Capture arbitrary path in Flask route
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 root directory (a leading slash … Read more
Get raw POST body in Python Flask regardless of Content-Type header
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.data first, it will call get_data with an argument to parse form data first. If the request has a form content type (multipart/form-data, application/x-www-form-urlencoded, or application/x-url-encoded) then … Read more
Is the server bundled with Flask safe to use in production?
No. The bundled server is a development server. It’s not designed with production environments in mind. It will not handle more than one request at a time by default. If you leave debug mode on and an error pops up, it opens up a shell that allows for arbitrary code to be executed on your … Read more
Get IP address of visitors using Flask for Python
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 import jsonify @app.route(“/get_my_ip”, methods=[“GET”]) def get_my_ip(): return jsonify({‘ip’: request.remote_addr}), 200 For more information see the Werkzeug documentation.