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

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