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

1.. currentmodule:: flask23Development Server4==================56Flask provides a ``run`` command to run the application with a development server. In7debug mode, this server provides an interactive debugger and will reload when code is8changed.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`` CLI command is the recommended way to run the development server. Use22the ``--app`` option to point to your application, and the ``--debug`` option to enable23debug mode.2425.. code-block:: text2627    $ flask --app hello run --debug2829This enables debug mode, including the interactive debugger and reloader, and then30starts 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.333435.. _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 port795000. To disable the service, go to System Preferences, Sharing, and80disable "AirPlay Receiver".818283Deferred Errors on Reload84~~~~~~~~~~~~~~~~~~~~~~~~~8586When 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-------99100The 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.104105Place the call in a main block, otherwise it will interfere when trying to import and106run the application with a production server later.107108.. code-block:: python109110    if __name__ == "__main__":111        app.run(debug=True)112113.. code-block:: text114115    $ python hello.py