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 fileTake it
1.. currentmodule:: flask23Development 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. Use the ``--app`` option to point to your23application, and the ``--env development`` option to fully enable24development mode.2526.. code-block:: text2728 $ flask --app hello --env development run2930These options (and any others) can also be set using environment31variables.3233.. tabs::3435 .. group-tab:: Bash3637 .. code-block:: text3839 $ export FLASK_APP=hello40 $ export FLASK_ENV=development41 $ flask run4243 .. group-tab:: Fish4445 .. code-block:: text4647 $ set -x FLASK_APP hello48 $ export FLASK_ENV=development49 $ flask run5051 .. group-tab:: CMD5253 .. 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::7475 Debug mode can be controlled separately from the development76 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.808182.. _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.9798You can use ``netstat`` or ``lsof`` to identify what process id is using99a port, then use other operating system tools stop that process. The100following example shows that process id 6847 is using port 5000.101102.. tabs::103104 .. tab:: ``netstat`` (Linux)105106 .. code-block:: text107108 $ netstat -nlp | grep 5000109 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python110111 .. tab:: ``lsof`` (macOS / Linux)112113 .. code-block:: text114115 $ lsof -P -i :5000116 Python 6847 IPv4 TCP localhost:5000 (LISTEN)117118 .. tab:: ``netstat`` (Windows)119120 .. code-block:: text121122 > netstat -ano | findstr 5000123 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847124125macOS Monterey and later automatically starts a service that uses port1265000. To disable the service, go to System Preferences, Sharing, and127disable "AirPlay Receiver".128129130Lazy or Eager Loading131~~~~~~~~~~~~~~~~~~~~~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