Skip to content
Myr

server.rst

This file

Path

docs/server.rst

157 lines · 4.7 KiB

Last changed by

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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–50why, one step back
28 .. group-tab:: Bash
2930 .. code-block:: text3132 $ export FLASK_APP=hello33 $ export FLASK_ENV=development34 $ flask run3536 .. group-tab:: CMD3738 .. code-block:: text3940 > set FLASK_APP=hello41 > set FLASK_ENV=development42 > flask run4344 .. group-tab:: Powershell4546 .. code-block:: text4748 > $env:FLASK_APP = "hello"49 > $env:FLASK_ENV = "development"50 > flask run

update metadataDavid Lordbefore adoption634e901

lines 51–66why, one step back
51
52This enables the development environment, including the interactive53debugger and reloader, and then starts the server on54http://localhost:5000/. Use ``flask run --help`` to see the available55options, and :doc:`/cli` for detailed instructions about configuring56and using the CLI.5758.. note::5960 Prior to Flask 1.0 the ``FLASK_ENV`` environment variable was not61 supported and you needed to enable debug mode by exporting62 ``FLASK_DEBUG=1``. This can still be used to control debug mode, but63 you should prefer setting the development environment as shown64 above.6566

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 67–82why, one step back
67.. _address-already-in-use:
6869Address already in use70~~~~~~~~~~~~~~~~~~~~~~7172If another program is already using port 5000, you'll see an ``OSError``73when the server tries to start. It may have one of the following74messages:7576- ``OSError: [Errno 98] Address already in use``77- ``OSError: [WinError 10013] An attempt was made to access a socket78 in a way forbidden by its access permissions``7980Either identify and stop the other program, or use81``flask run --port 5001`` to pick a different port.82

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 86–88why, one step back
86
87.. tabs::88

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 90–95why, one step back
90
91 .. code-block:: text9293 $ netstat -nlp | grep 500094 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python95

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 97–99why, one step back
97
98 .. code-block:: text99

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

lines 100–106why, one step back
100 $ lsof -P -i :5000
101 Python 6847 IPv4 TCP localhost:5000 (LISTEN)102103 .. tab:: ``netstat`` (Windows)104105 .. code-block:: text106

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 107–109why, one step back
107 > netstat -ano | findstr 5000
108 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847109

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 111–114why, one step back
1115000. To disable the service, go to System Preferences, Sharing, and
112disable "AirPlay Receiver".113114

update metadataDavid Lordbefore adoption634e901

lines 115–157why, one step back
115Lazy or Eager Loading
116~~~~~~~~~~~~~~~~~~~~~117118When using the ``flask run`` command with the reloader, the server will119continue to run even if you introduce syntax errors or other120initialization errors into the code. Accessing the site will show the121interactive debugger for the error, rather than crashing the server.122This feature is called "lazy loading".123124If a syntax error is already present when calling ``flask run``, it will125fail immediately and show the traceback rather than waiting until the126site is accessed. This is intended to make errors more visible initially127while still allowing the server to handle errors on reload.128129To override this behavior and always fail immediately, even on reload,130pass the ``--eager-loading`` option. To always keep the server running,131even on the initial call, pass ``--lazy-loading``.132133134In Code135-------136137As an alternative to the ``flask run`` command, the development server138can also be started from Python with the :meth:`Flask.run` method. This139method takes arguments similar to the CLI options to control the server.140The main difference from the CLI command is that the server will crash141if there are errors when reloading.142143``debug=True`` can be passed to enable the debugger and reloader, but144the ``FLASK_ENV=development`` environment variable is still required to145fully enable development mode.146147Place the call in a main block, otherwise it will interfere when trying148to import and run the application with a production server later.149150.. code-block:: python151152 if __name__ == "__main__":153 app.run(debug=True)154155.. code-block:: text156157 $ python hello.py