Skip to content
Myr

server.rst

This file

Path

docs/server.rst

172 lines · 5.0 KiB

Last changed by

Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727

History of this file

Take it

The walk stopped after 200 commits: lines older than aa0c949 point at it.

Bump tox from 3.20.0 to 3.20.1 (#3819)dependabot-preview[bot]before adoptionaa0c949

lines 1–21why, one step back
1.. currentmodule:: flask
23Development Server4==================56Flask provides a ``run`` command to run the application with a7development server. In development mode, this server provides an8interactive debugger and will reload when code is changed.910.. warning::1112 Do not use the development server when deploying to production. It13 is intended for use only during local development. It is not14 designed to be particularly efficient, stable, or secure.1516 See :doc:`/deploying/index` for deployment options.1718Command Line19------------2021The ``flask run`` command line script is the recommended way to run the

Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727

lines 22–24why, one step back
22development server. Use the ``--app`` option to point to your
23application, and the ``--env development`` option to fully enable24development mode.

Bump tox from 3.20.0 to 3.20.1 (#3819)dependabot-preview[bot]before adoptionaa0c949

line 25why, one step back
25

Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727

lines 26–32why, one step back
26.. code-block:: text
2728 $ flask --app hello --env development run2930These options (and any others) can also be set using environment31variables.32

Bump tox from 3.20.0 to 3.20.1 (#3819)dependabot-preview[bot]before adoptionaa0c949

lines 33–42why, one step back
33.. tabs::
3435 .. group-tab:: Bash3637 .. code-block:: text3839 $ export FLASK_APP=hello40 $ export FLASK_ENV=development41 $ flask run42

Merge branch '2.0.x'David Lordbefore adoptione37e871

lines 43–50why, one step back
43 .. group-tab:: Fish
4445 .. code-block:: text4647 $ set -x FLASK_APP hello48 $ export FLASK_ENV=development49 $ flask run50

Bump tox from 3.20.0 to 3.20.1 (#3819)dependabot-preview[bot]before adoptionaa0c949

lines 51–74why, one step back
51 .. group-tab:: CMD
5253 .. code-block:: text5455 > set FLASK_APP=hello56 > set FLASK_ENV=development57 > flask run5859 .. group-tab:: Powershell6061 .. code-block:: text6263 > $env:FLASK_APP = "hello"64 > $env:FLASK_ENV = "development"65 > flask run6667This enables the development environment, including the interactive68debugger and reloader, and then starts the server on69http://localhost:5000/. Use ``flask run --help`` to see the available70options, and :doc:`/cli` for detailed instructions about configuring71and using the CLI.7273.. note::74

Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727

lines 75–79why, one step back
75 Debug mode can be controlled separately from the development
76 environment with the ``--debug/--no-debug`` option or the77 ``FLASK_DEBUG`` environment variable. This is how older versions of78 Flask worked. You should prefer setting the development environment79 as shown above.

Bump tox from 3.20.0 to 3.20.1 (#3819)dependabot-preview[bot]before adoptionaa0c949

lines 80–81why, one step back
80
81

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 82–97why, one step back
82.. _address-already-in-use:
8384Address already in use85~~~~~~~~~~~~~~~~~~~~~~8687If another program is already using port 5000, you'll see an ``OSError``88when the server tries to start. It may have one of the following89messages:9091- ``OSError: [Errno 98] Address already in use``92- ``OSError: [WinError 10013] An attempt was made to access a socket93 in a way forbidden by its access permissions``9495Either identify and stop the other program, or use96``flask run --port 5001`` to pick a different port.97

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

lines 98–100why, one step back
98You can use ``netstat`` or ``lsof`` to identify what process id is using
99a port, then use other operating system tools stop that process. The100following example shows that process id 6847 is using port 5000.

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 101–103why, one step back
101
102.. tabs::103

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

line 104why, one step back
104 .. tab:: ``netstat`` (Linux)

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 105–110why, one step back
105
106 .. code-block:: text107108 $ netstat -nlp | grep 5000109 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python110

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

line 111why, one step back
111 .. tab:: ``lsof`` (macOS / Linux)

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 112–114why, one step back
112
113 .. code-block:: text114

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

lines 115–121why, one step back
115 $ lsof -P -i :5000
116 Python 6847 IPv4 TCP localhost:5000 (LISTEN)117118 .. tab:: ``netstat`` (Windows)119120 .. code-block:: text121

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 122–124why, one step back
122 > netstat -ano | findstr 5000
123 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847124

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

line 125why, one step back
125macOS Monterey and later automatically starts a service that uses port

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 126–129why, one step back
1265000. To disable the service, go to System Preferences, Sharing, and
127disable "AirPlay Receiver".128129

Bump tox from 3.20.0 to 3.20.1 (#3819)dependabot-preview[bot]before adoptionaa0c949

lines 130–172why, one step back
130Lazy or Eager Loading
131~~~~~~~~~~~~~~~~~~~~~132133When using the ``flask run`` command with the reloader, the server will134continue to run even if you introduce syntax errors or other135initialization errors into the code. Accessing the site will show the136interactive debugger for the error, rather than crashing the server.137This feature is called "lazy loading".138139If a syntax error is already present when calling ``flask run``, it will140fail immediately and show the traceback rather than waiting until the141site is accessed. This is intended to make errors more visible initially142while still allowing the server to handle errors on reload.143144To override this behavior and always fail immediately, even on reload,145pass the ``--eager-loading`` option. To always keep the server running,146even on the initial call, pass ``--lazy-loading``.147148149In Code150-------151152As an alternative to the ``flask run`` command, the development server153can also be started from Python with the :meth:`Flask.run` method. This154method takes arguments similar to the CLI options to control the server.155The main difference from the CLI command is that the server will crash156if there are errors when reloading.157158``debug=True`` can be passed to enable the debugger and reloader, but159the ``FLASK_ENV=development`` environment variable is still required to160fully enable development mode.161162Place the call in a main block, otherwise it will interfere when trying163to import and run the application with a production server later.164165.. code-block:: python166167 if __name__ == "__main__":168 app.run(debug=True)169170.. code-block:: text171172 $ python hello.py