<p dir="auto">Simplest sample program:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from flask import Flask

app = Flask(&quot;TestApp&quot;)

@app.route(&quot;/&quot;)
def test():
    return &quot;Hello World!&quot;

app.run(host=&quot;0.0.0.0&quot;, port=5001, ssl_context=&quot;adhoc&quot;)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">Flask</span>(<span class="pl-s">"TestApp"</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test</span>():
    <span class="pl-k">return</span> <span class="pl-s">"Hello World!"</span>

<span class="pl-s1">app</span>.<span class="pl-en">run</span>(<span class="pl-s1">host</span><span class="pl-c1">=</span><span class="pl-s">"0.0.0.0"</span>, <span class="pl-s1">port</span><span class="pl-c1">=</span><span class="pl-c1">5001</span>, <span class="pl-s1">ssl_context</span><span class="pl-c1">=</span><span class="pl-s">"adhoc"</span>)</pre></div>
<p dir="auto">Test the web server through a browser: https://ip_address:5001/<br>
The result is: <code class="notranslate">Hello World!</code>.</p>
<p dir="auto">Run <code class="notranslate">telnet ip_address 5001</code> (do not add chars as it expects an SSL/TLS session)<br>
Test the web server through a browser: https://ip_address:5001/</p>
<p dir="auto">The browser request hangs until the telnet is killed.</p>
<p dir="auto">If you use <code class="notranslate">app.run(host="0.0.0.0", port=5001, ssl_context="adhoc", threaded=True)</code> the result is the same: each request is decoded before spawning the thread.</p>
<p dir="auto">Simplest example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import time
from flask import Flask

app = Flask(&quot;TestApp&quot;)

@app.route(&quot;/0&quot;)
def test_0():
    time.sleep(10)
    return &quot;Hello World-0!&quot;

@app.route(&quot;/1&quot;)
def test_1():
    time.sleep(10)
    return &quot;Hello World-1!&quot;

@app.route(&quot;/2&quot;)
def test_2():
    time.sleep(10)
    return &quot;Hello World-2!&quot;

app.run(host=&quot;0.0.0.0&quot;, port=5001, ssl_context=&quot;adhoc&quot;, threaded=True)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">time</span>
<span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">Flask</span>(<span class="pl-s">"TestApp"</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/0"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test_0</span>():
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">10</span>)
    <span class="pl-k">return</span> <span class="pl-s">"Hello World-0!"</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/1"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test_1</span>():
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">10</span>)
    <span class="pl-k">return</span> <span class="pl-s">"Hello World-1!"</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/2"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test_2</span>():
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">10</span>)
    <span class="pl-k">return</span> <span class="pl-s">"Hello World-2!"</span>

<span class="pl-s1">app</span>.<span class="pl-en">run</span>(<span class="pl-s1">host</span><span class="pl-c1">=</span><span class="pl-s">"0.0.0.0"</span>, <span class="pl-s1">port</span><span class="pl-c1">=</span><span class="pl-c1">5001</span>, <span class="pl-s1">ssl_context</span><span class="pl-c1">=</span><span class="pl-s">"adhoc"</span>, <span class="pl-s1">threaded</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<p dir="auto">Run <code class="notranslate">telnet ip_address 5001</code><br>
Test the web server through a browser on different tabs using different routes to enable multithreading:</p>
<ul dir="auto">
<li>https://ip_address:5001/0</li>
<li>https://ip_address:5001/1</li>
<li>https://ip_address:5001/2</li>
</ul>
<p dir="auto">All requests hang until the telnet is killed.</p>
<p dir="auto">It looks like when using <code class="notranslate">ssl_context</code> each request is strictly decoded in sequence.</p>
<p dir="auto">Environment:</p>
<ul dir="auto">
<li>Python version: 3.10</li>
<li>Flask version: 2.2.2</li>
</ul>
<p dir="auto">It is also a security issue. The attack client for an <em>ssl_context</em> based web app is:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from telnetlib import Telnet

hostname = '...'
port = 5001

while True:
    with Telnet(hostname, port) as tn:
        tn.read_some()"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">telnetlib</span> <span class="pl-k">import</span> <span class="pl-v">Telnet</span>

<span class="pl-s1">hostname</span> <span class="pl-c1">=</span> <span class="pl-s">'...'</span>
<span class="pl-s1">port</span> <span class="pl-c1">=</span> <span class="pl-c1">5001</span>

<span class="pl-k">while</span> <span class="pl-c1">True</span>:
    <span class="pl-k">with</span> <span class="pl-v">Telnet</span>(<span class="pl-s1">hostname</span>, <span class="pl-s1">port</span>) <span class="pl-k">as</span> <span class="pl-s1">tn</span>:
        <span class="pl-s1">tn</span>.<span class="pl-en">read_some</span>()</pre></div>
<p dir="auto">As a temporary workaround, this can be added at the beginning of the Flask program:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import socket
socket.setdefaulttimeout(5)  # seconds"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">socket</span>
<span class="pl-s1">socket</span>.<span class="pl-en">setdefaulttimeout</span>(<span class="pl-c1">5</span>)  <span class="pl-c"># seconds</span></pre></div>
<p dir="auto">Notice that this program works while <code class="notranslate">telnet ip_address 5000</code> runs, because <code class="notranslate">ssl_context</code> is not used:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import time
from flask import Flask

app = Flask(&quot;TestApp&quot;)

@app.route(&quot;/0&quot;)
def test_0():
    time.sleep(10)
    return &quot;Hello World-0!&quot;

@app.route(&quot;/1&quot;)
def test_1():
    time.sleep(10)
    return &quot;Hello World-1!&quot;

@app.route(&quot;/2&quot;)
def test_2():
    time.sleep(10)
    return &quot;Hello World-2!&quot;

app.run(host=&quot;0.0.0.0&quot;, port=5000, threaded=True)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">time</span>
<span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">Flask</span>(<span class="pl-s">"TestApp"</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/0"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test_0</span>():
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">10</span>)
    <span class="pl-k">return</span> <span class="pl-s">"Hello World-0!"</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/1"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test_1</span>():
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">10</span>)
    <span class="pl-k">return</span> <span class="pl-s">"Hello World-1!"</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">"/2"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">test_2</span>():
    <span class="pl-s1">time</span>.<span class="pl-en">sleep</span>(<span class="pl-c1">10</span>)
    <span class="pl-k">return</span> <span class="pl-s">"Hello World-2!"</span>

<span class="pl-s1">app</span>.<span class="pl-en">run</span>(<span class="pl-s1">host</span><span class="pl-c1">=</span><span class="pl-s">"0.0.0.0"</span>, <span class="pl-s1">port</span><span class="pl-c1">=</span><span class="pl-c1">5000</span>, <span class="pl-s1">threaded</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<p dir="auto">It would be useful to be able to refer to <a href="http://flask.pocoo.org/docs/patterns/flashing/" rel="nofollow">flash categories</a> using constants, for example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="FLASH_CATEGORY_MESSAGE = 'message'
FLASH_CATEGORY_DEFAULT = FLASH_CATEGORY_MESSAGE"><pre class="notranslate"><code class="notranslate">FLASH_CATEGORY_MESSAGE = 'message'
FLASH_CATEGORY_DEFAULT = FLASH_CATEGORY_MESSAGE
</code></pre></div>
<p dir="auto">This gives an overview of the <strong>already defined</strong> categories, and an obvious way to avoid duplicating these in every Flask application.</p>