Skip to content
Myr

server.rst

This file

Path

docs/server.rst

115 lines · 3.6 KiB

Last changed by

Merge pull request #4980 from pallets/release-2.2.3David Lordbefore adoption6a392af

History of this file

Take it

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

Merge remote-tracking branch 'origin/2.0.x'David Lordbefore adoption4240ace

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.0.x'David Lordbefore adoption4240ace

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.0.x'David Lordbefore adoption4240ace

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

Merge pull request #4980 from pallets/release-2.2.3David Lordbefore adoption6a392af

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.0.x'David Lordbefore adoption4240ace

line 34why, one step back
34

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 35–50why, one step back
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.50

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 54–56why, one step back
54
55.. tabs::56

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 58–63why, one step back
58
59 .. code-block:: text6061 $ netstat -nlp | grep 500062 tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python63

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 65–67why, one step back
65
66 .. code-block:: text67

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

lines 68–74why, one step back
68 $ lsof -P -i :5000
69 Python 6847 IPv4 TCP localhost:5000 (LISTEN)7071 .. tab:: ``netstat`` (Windows)7273 .. code-block:: text74

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 75–77why, one step back
75 > netstat -ano | findstr 5000
76 TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 684777

Merge branch '2.0.x'David Lordbefore adoption0fb5c2f

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

Merge branch '2.0.x'David Lordbefore adoptionedac7e3

lines 79–82why, one step back
795000. To disable the service, go to System Preferences, Sharing, and
80disable "AirPlay Receiver".8182

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.0.x'David Lordbefore adoption4240ace

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.0.x'David Lordbefore adoption4240ace

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.0.x'David Lordbefore adoption4240ace

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