Skip to content
Myr

server.rst

This file

Path

docs/server.rst

115 lines · 3.6 KiB

Last changed by

Merge branch '3.0.x'David Lordbefore adoption12a1c49

History of this file

Take it

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

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 1–5why, one step back
1.. currentmodule:: flask
23Development Server4==================5

Merge pull request #4720 from pallets/deprecate-envDavid Lordbefore adoption98ca00d

lines 6–8why, one step back
6Flask provides a ``run`` command to run the application with a development server. In
7debug mode, this server provides an interactive debugger and will reload when code is8changed.

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 9–20why, one step back
9
10.. 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------------20

Merge pull request #4720 from pallets/deprecate-envDavid Lordbefore adoption98ca00d

lines 21–23why, one step back
21The ``flask run`` CLI command is the recommended way to run the development server. Use
22the ``--app`` option to point to your application, and the ``--debug`` option to enable23debug mode.

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

line 24why, one step back
24

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

lines 25–26why, one step back
25.. code-block:: text
26

new run debug docsDavid Lordbefore adoption74e5263

line 27why, one step back
27 $ flask --app hello run --debug

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

line 28why, one step back
28

Merge pull request #4720 from pallets/deprecate-envDavid Lordbefore adoption98ca00d

lines 29–32why, one step back
29This enables debug mode, including the interactive debugger and reloader, and then
30starts the server on http://localhost:5000/. Use ``flask run --help`` to see the31available options, and :doc:`/cli` for detailed instructions about configuring and using32the CLI.

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

line 33why, one step back
33

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 34–78why, one step back
34
35.. _address-already-in-use:3637Address already in use38~~~~~~~~~~~~~~~~~~~~~~3940If another program is already using port 5000, you'll see an ``OSError``41when the server tries to start. It may have one of the following42messages:4344- ``OSError: [Errno 98] Address already in use``45- ``OSError: [WinError 10013] An attempt was made to access a socket46 in a way forbidden by its access permissions``4748Either identify and stop the other program, or use49``flask run --port 5001`` to pick a different port.5051You can use ``netstat`` or ``lsof`` to identify what process id is using52a port, then use other operating system tools stop that process. The53following example shows that process id 6847 is using port 5000.5455.. tabs::5657 .. tab:: ``netstat`` (Linux)5859 .. code-block:: text6061 $ netstat -nlp | grep 500062 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python6364 .. tab:: ``lsof`` (macOS / Linux)6566 .. code-block:: text6768 $ lsof -P -i :500069 Python 6847 IPv4 TCP localhost:5000 (LISTEN)7071 .. tab:: ``netstat`` (Windows)7273 .. code-block:: text7475 > netstat -ano | findstr 500076 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 68477778macOS Monterey and later automatically starts a service that uses port

Merge branch '3.0.x'David Lordbefore adoption12a1c49

lines 79–80why, one step back
795000. You can choose to disable this service instead of using a different port by
80searching for "AirPlay Receiver" in System Preferences and toggling it off.

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 81–82why, one step back
81
82

Merge pull request #4718 from pallets/remove-lazy-optionDavid Lordbefore adoption633f421

lines 83–84why, one step back
83Deferred Errors on Reload
84~~~~~~~~~~~~~~~~~~~~~~~~~

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 85–99why, one step back
85
86When using the ``flask run`` command with the reloader, the server will87continue to run even if you introduce syntax errors or other88initialization errors into the code. Accessing the site will show the89interactive debugger for the error, rather than crashing the server.9091If a syntax error is already present when calling ``flask run``, it will92fail immediately and show the traceback rather than waiting until the93site is accessed. This is intended to make errors more visible initially94while still allowing the server to handle errors on reload.959697In Code98-------99

Merge pull request #4720 from pallets/deprecate-envDavid Lordbefore adoption98ca00d

lines 100–103why, one step back
100The development server can also be started from Python with the :meth:`Flask.run`
101method. This method takes arguments similar to the CLI options to control the server.102The main difference from the CLI command is that the server will crash if there are103errors when reloading. ``debug=True`` can be passed to enable debug mode.

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

line 104why, one step back
104

Merge pull request #4720 from pallets/deprecate-envDavid Lordbefore adoption98ca00d

lines 105–106why, one step back
105Place the call in a main block, otherwise it will interfere when trying to import and
106run the application with a production server later.

Merge remote-tracking branch 'origin/2.1.x'David Lordbefore adoption1888df3

lines 107–115why, one step back
107
108.. code-block:: python109110 if __name__ == "__main__":111 app.run(debug=True)112113.. code-block:: text114115 $ python hello.py