flask/flask-docs/reqcontext.html
2025-04-11 03:04:22 +00:00

190 lines
12 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Development Server &#8212; Flask Documentation (3.2.x)</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=6625fa76" />
<link rel="stylesheet" type="text/css" href="_static/flask.css?v=b87c8d14" />
<link rel="stylesheet" type="text/css" href="_static/tabs.css?v=a5c4661c" />
<script src="_static/documentation_options.js?v=56528222"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script data-project="flask" data-version="3.2.x" src="_static/describe_version.js?v=fa7f30d0"></script>
<script src="_static/tabs.js?v=3030b3cb"></script>
<link rel="icon" href="_static/shortcut-icon.png"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Working with the Shell" href="shell.html" />
<link rel="prev" title="Command Line Interface" href="cli.html" />
</head><body>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="shell.html" title="Working with the Shell"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="cli.html" title="Command Line Interface"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Flask Documentation (3.2.x)</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">Development Server</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="development-server">
<h1>Development Server<a class="headerlink" href="#development-server" title="Link to this heading"></a></h1>
<p>Flask provides a <code class="docutils literal notranslate"><span class="pre">run</span></code> command to run the application with a development server. In
debug mode, this server provides an interactive debugger and will reload when code is
changed.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Do not use the development server when deploying to production. It
is intended for use only during local development. It is not
designed to be particularly efficient, stable, or secure.</p>
<p>See <a class="reference internal" href="deploying/index.html"><span class="doc">Deploying to Production</span></a> for deployment options.</p>
</div>
<section id="command-line">
<h2>Command Line<a class="headerlink" href="#command-line" title="Link to this heading"></a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">flask</span> <span class="pre">run</span></code> CLI command is the recommended way to run the development server. Use
the <code class="docutils literal notranslate"><span class="pre">--app</span></code> option to point to your application, and the <code class="docutils literal notranslate"><span class="pre">--debug</span></code> option to enable
debug mode.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>$ flask --app hello run --debug
</pre></div>
</div>
<p>This enables debug mode, including the interactive debugger and reloader, and then
starts the server on <a class="reference external" href="http://localhost:5000/">http://localhost:5000/</a>. Use <code class="docutils literal notranslate"><span class="pre">flask</span> <span class="pre">run</span> <span class="pre">--help</span></code> to see the
available options, and <a class="reference internal" href="cli.html"><span class="doc">Command Line Interface</span></a> for detailed instructions about configuring and using
the CLI.</p>
<section id="address-already-in-use">
<span id="id1"></span><h3>Address already in use<a class="headerlink" href="#address-already-in-use" title="Link to this heading"></a></h3>
<p>If another program is already using port 5000, youll see an <code class="docutils literal notranslate"><span class="pre">OSError</span></code>
when the server tries to start. It may have one of the following
messages:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">OSError:</span> <span class="pre">[Errno</span> <span class="pre">98]</span> <span class="pre">Address</span> <span class="pre">already</span> <span class="pre">in</span> <span class="pre">use</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">OSError:</span> <span class="pre">[WinError</span> <span class="pre">10013]</span> <span class="pre">An</span> <span class="pre">attempt</span> <span class="pre">was</span> <span class="pre">made</span> <span class="pre">to</span> <span class="pre">access</span> <span class="pre">a</span> <span class="pre">socket</span>
<span class="pre">in</span> <span class="pre">a</span> <span class="pre">way</span> <span class="pre">forbidden</span> <span class="pre">by</span> <span class="pre">its</span> <span class="pre">access</span> <span class="pre">permissions</span></code></p></li>
</ul>
<p>Either identify and stop the other program, or use
<code class="docutils literal notranslate"><span class="pre">flask</span> <span class="pre">run</span> <span class="pre">--port</span> <span class="pre">5001</span></code> to pick a different port.</p>
<p>You can use <code class="docutils literal notranslate"><span class="pre">netstat</span></code> or <code class="docutils literal notranslate"><span class="pre">lsof</span></code> to identify what process id is using
a port, then use other operating system tools stop that process. The
following example shows that process id 6847 is using port 5000.</p>
<div class="sphinx-tabs docutils container">
<div aria-label="Tabbed content" class="closeable" role="tablist"><button aria-controls="panel-0-0-0" aria-selected="true" class="sphinx-tabs-tab" id="tab-0-0-0" name="0-0" role="tab" tabindex="0"><code class="docutils literal notranslate"><span class="pre">netstat</span></code> (Linux)</button><button aria-controls="panel-0-0-1" aria-selected="false" class="sphinx-tabs-tab" id="tab-0-0-1" name="0-1" role="tab" tabindex="-1"><code class="docutils literal notranslate"><span class="pre">lsof</span></code> (macOS / Linux)</button><button aria-controls="panel-0-0-2" aria-selected="false" class="sphinx-tabs-tab" id="tab-0-0-2" name="0-2" role="tab" tabindex="-1"><code class="docutils literal notranslate"><span class="pre">netstat</span></code> (Windows)</button></div><div aria-labelledby="tab-0-0-0" class="sphinx-tabs-panel" id="panel-0-0-0" name="0-0" role="tabpanel" tabindex="0"><div class="highlight-text notranslate"><div class="highlight"><pre><span></span>$ netstat -nlp | grep 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python
</pre></div>
</div>
</div><div aria-labelledby="tab-0-0-1" class="sphinx-tabs-panel" hidden="true" id="panel-0-0-1" name="0-1" role="tabpanel" tabindex="0"><div class="highlight-text notranslate"><div class="highlight"><pre><span></span>$ lsof -P -i :5000
Python 6847 IPv4 TCP localhost:5000 (LISTEN)
</pre></div>
</div>
</div><div aria-labelledby="tab-0-0-2" class="sphinx-tabs-panel" hidden="true" id="panel-0-0-2" name="0-2" role="tabpanel" tabindex="0"><div class="highlight-text notranslate"><div class="highlight"><pre><span></span>&gt; netstat -ano | findstr 5000
TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847
</pre></div>
</div>
</div></div>
<p>macOS Monterey and later automatically starts a service that uses port
5000. You can choose to disable this service instead of using a different port by
searching for “AirPlay Receiver” in System Preferences and toggling it off.</p>
</section>
<section id="deferred-errors-on-reload">
<h3>Deferred Errors on Reload<a class="headerlink" href="#deferred-errors-on-reload" title="Link to this heading"></a></h3>
<p>When using the <code class="docutils literal notranslate"><span class="pre">flask</span> <span class="pre">run</span></code> command with the reloader, the server will
continue to run even if you introduce syntax errors or other
initialization errors into the code. Accessing the site will show the
interactive debugger for the error, rather than crashing the server.</p>
<p>If a syntax error is already present when calling <code class="docutils literal notranslate"><span class="pre">flask</span> <span class="pre">run</span></code>, it will
fail immediately and show the traceback rather than waiting until the
site is accessed. This is intended to make errors more visible initially
while still allowing the server to handle errors on reload.</p>
</section>
</section>
<section id="in-code">
<h2>In Code<a class="headerlink" href="#in-code" title="Link to this heading"></a></h2>
<p>The development server can also be started from Python with the <a class="reference internal" href="api.html#flask.Flask.run" title="flask.Flask.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Flask.run()</span></code></a>
method. This method takes arguments similar to the CLI options to control the server.
The main difference from the CLI command is that the server will crash if there are
errors when reloading. <code class="docutils literal notranslate"><span class="pre">debug=True</span></code> can be passed to enable debug mode.</p>
<p>Place the call in a main block, otherwise it will interfere when trying to import and
run the application with a production server later.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">debug</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>$ python hello.py
</pre></div>
</div>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<span id="sidebar-top"></span>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/flask-vertical.png" alt="Logo of Flask"/>
</a></p>
<h3>Contents</h3>
<ul>
<li><a class="reference internal" href="#">Development Server</a><ul>
<li><a class="reference internal" href="#command-line">Command Line</a><ul>
<li><a class="reference internal" href="#address-already-in-use">Address already in use</a></li>
<li><a class="reference internal" href="#deferred-errors-on-reload">Deferred Errors on Reload</a></li>
</ul>
</li>
<li><a class="reference internal" href="#in-code">In Code</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
<li><a href="index.html">Overview</a>
<ul>
<li>Previous: <a href="cli.html" title="previous chapter">Command Line Interface</a>
<li>Next: <a href="shell.html" title="next chapter">Working with the Shell</a>
</ul>
</li>
</ul>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script><div id="ethical-ad-placement"></div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright 2010 Pallets.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
</div>
</body>
</html>