Skip to content
Myr

server.rst

This file

Path

docs/server.rst

165 lines · 4.9 KiB

Last changed by

Merge branch '2.0.x'David Lordbefore adoptione37e871

History of this file

Take it

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

update metadataDavid Lordbefore adoption634e901

lines 1–25why, 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 the22development server. It requires setting the ``FLASK_APP`` environment23variable to point to your application, and ``FLASK_ENV=development`` to24fully enable development mode.25

Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc

line 26why, one step back
26.. tabs::

update metadataDavid Lordbefore adoption634e901

line 27why, one step back
27

Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc

lines 28–35why, one step back
28 .. group-tab:: Bash
2930 .. code-block:: text3132 $ export FLASK_APP=hello33 $ export FLASK_ENV=development34 $ flask run35

Merge branch '2.0.x'David Lordbefore adoptione37e871

lines 36–43why, one step back
36 .. group-tab:: Fish
3738 .. code-block:: text3940 $ set -x FLASK_APP hello41 $ export FLASK_ENV=development42 $ flask run43

Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc

lines 44–58why, one step back
44 .. group-tab:: CMD
4546 .. code-block:: text4748 > set FLASK_APP=hello49 > set FLASK_ENV=development50 > flask run5152 .. group-tab:: Powershell5354 .. code-block:: text5556 > $env:FLASK_APP = "hello"57 > $env:FLASK_ENV = "development"58 > flask run

update metadataDavid Lordbefore adoption634e901

lines 59–74why, one step back
59
60This enables the development environment, including the interactive61debugger and reloader, and then starts the server on62http://localhost:5000/. Use ``flask run --help`` to see the available63options, and :doc:`/cli` for detailed instructions about configuring64and using the CLI.6566.. note::6768 Prior to Flask 1.0 the ``FLASK_ENV`` environment variable was not69 supported and you needed to enable debug mode by exporting70 ``FLASK_DEBUG=1``. This can still be used to control debug mode, but71 you should prefer setting the development environment as shown72 above.7374

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 75–90why, one step back
75.. _address-already-in-use:
7677Address already in use78~~~~~~~~~~~~~~~~~~~~~~7980If another program is already using port 5000, you'll see an ``OSError``81when the server tries to start. It may have one of the following82messages:8384- ``OSError: [Errno 98] Address already in use``85- ``OSError: [WinError 10013] An attempt was made to access a socket86 in a way forbidden by its access permissions``8788Either identify and stop the other program, or use89``flask run --port 5001`` to pick a different port.90

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 94–96why, one step back
94
95.. tabs::96

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 98–103why, one step back
98
99 .. code-block:: text100101 $ netstat -nlp | grep 5000102 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python103

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 105–107why, one step back
105
106 .. code-block:: text107

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

lines 108–114why, one step back
108 $ lsof -P -i :5000
109 Python 6847 IPv4 TCP localhost:5000 (LISTEN)110111 .. tab:: ``netstat`` (Windows)112113 .. code-block:: text114

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 115–117why, one step back
115 > netstat -ano | findstr 5000
116 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847117

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 119–122why, one step back
1195000. To disable the service, go to System Preferences, Sharing, and
120disable "AirPlay Receiver".121122

update metadataDavid Lordbefore adoption634e901

lines 123–165why, one step back
123Lazy or Eager Loading
124~~~~~~~~~~~~~~~~~~~~~125126When using the ``flask run`` command with the reloader, the server will127continue to run even if you introduce syntax errors or other128initialization errors into the code. Accessing the site will show the129interactive debugger for the error, rather than crashing the server.130This feature is called "lazy loading".131132If a syntax error is already present when calling ``flask run``, it will133fail immediately and show the traceback rather than waiting until the134site is accessed. This is intended to make errors more visible initially135while still allowing the server to handle errors on reload.136137To override this behavior and always fail immediately, even on reload,138pass the ``--eager-loading`` option. To always keep the server running,139even on the initial call, pass ``--lazy-loading``.140141142In Code143-------144145As an alternative to the ``flask run`` command, the development server146can also be started from Python with the :meth:`Flask.run` method. This147method takes arguments similar to the CLI options to control the server.148The main difference from the CLI command is that the server will crash149if there are errors when reloading.150151``debug=True`` can be passed to enable the debugger and reloader, but152the ``FLASK_ENV=development`` environment variable is still required to153fully enable development mode.154155Place the call in a main block, otherwise it will interfere when trying156to import and run the application with a production server later.157158.. code-block:: python159160 if __name__ == "__main__":161 app.run(debug=True)162163.. code-block:: text164165 $ python hello.py