| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | .. currentmodule:: flask
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Development Server
 | 
					
						
							|  |  |  | ==================
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 06:11:35 +08:00
										 |  |  | Flask provides a ``run`` command to run the application with a development server. In
 | 
					
						
							|  |  |  | debug mode, this server provides an interactive debugger and will reload when code is
 | 
					
						
							|  |  |  | changed.
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. warning::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Do not use the development server when deploying to production. It
 | 
					
						
							|  |  |  |     is intended for use only during local development. It is not
 | 
					
						
							|  |  |  |     designed to be particularly efficient, stable, or secure.
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  |     See :doc:`/deploying/index` for deployment options.
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Command Line
 | 
					
						
							|  |  |  | ------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 06:11:35 +08:00
										 |  |  | The ``flask run`` CLI command is the recommended way to run the development server. Use
 | 
					
						
							|  |  |  | the ``--app`` option to point to your application, and the ``--debug`` option to enable
 | 
					
						
							|  |  |  | debug mode.
 | 
					
						
							| 
									
										
										
										
											2022-06-18 00:26:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. code-block:: text
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-30 01:52:18 +08:00
										 |  |  |     $ flask --app hello run --debug
 | 
					
						
							| 
									
										
										
										
											2022-06-18 00:26:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 06:11:35 +08:00
										 |  |  | This enables debug mode, including the interactive debugger and reloader, and then
 | 
					
						
							|  |  |  | starts the server on http://localhost:5000/. Use ``flask run --help`` to see the
 | 
					
						
							|  |  |  | available options, and :doc:`/cli` for detailed instructions about configuring and using
 | 
					
						
							|  |  |  | the CLI.
 | 
					
						
							| 
									
										
										
										
											2018-01-07 00:07:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 00:38:20 +08:00
										 |  |  | .. _address-already-in-use:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Address already in use
 | 
					
						
							|  |  |  | ~~~~~~~~~~~~~~~~~~~~~~
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If another program is already using port 5000, you'll see an ``OSError``
 | 
					
						
							|  |  |  | when the server tries to start. It may have one of the following
 | 
					
						
							|  |  |  | messages:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   ``OSError: [Errno 98] Address already in use``
 | 
					
						
							|  |  |  | -   ``OSError: [WinError 10013] An attempt was made to access a socket
 | 
					
						
							|  |  |  |     in a way forbidden by its access permissions``
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Either identify and stop the other program, or use
 | 
					
						
							|  |  |  | ``flask run --port 5001`` to pick a different port.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 12:05:44 +08:00
										 |  |  | You can use ``netstat`` or ``lsof`` to identify what process id is using
 | 
					
						
							|  |  |  | a port, then use other operating system tools stop that process. The
 | 
					
						
							|  |  |  | following example shows that process id 6847 is using port 5000.
 | 
					
						
							| 
									
										
										
										
											2021-11-17 00:38:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. tabs::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 12:05:44 +08:00
										 |  |  |     .. tab:: ``netstat`` (Linux)
 | 
					
						
							| 
									
										
										
										
											2021-11-17 00:38:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         .. code-block:: text
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $ netstat -nlp | grep 5000
 | 
					
						
							|  |  |  |             tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 12:05:44 +08:00
										 |  |  |     .. tab:: ``lsof`` (macOS / Linux)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         .. code-block:: text
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $ lsof -P -i :5000
 | 
					
						
							|  |  |  |             Python 6847 IPv4 TCP localhost:5000 (LISTEN)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .. tab:: ``netstat`` (Windows)
 | 
					
						
							| 
									
										
										
										
											2021-11-17 00:38:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         .. code-block:: text
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             > netstat -ano | findstr 5000
 | 
					
						
							|  |  |  |             TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING 6847
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-12 12:05:44 +08:00
										 |  |  | macOS Monterey and later automatically starts a service that uses port
 | 
					
						
							| 
									
										
										
										
											2023-12-06 01:58:59 +08:00
										 |  |  | 5000. You can choose to disable this service instead of using a different port by
 | 
					
						
							|  |  |  | searching for "AirPlay Receiver" in System Preferences and toggling it off.
 | 
					
						
							| 
									
										
										
										
											2021-11-17 00:38:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 00:13:01 +08:00
										 |  |  | Deferred Errors on Reload
 | 
					
						
							|  |  |  | ~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | When using the ``flask run`` command with the reloader, the server will
 | 
					
						
							|  |  |  | continue to run even if you introduce syntax errors or other
 | 
					
						
							|  |  |  | initialization errors into the code. Accessing the site will show the
 | 
					
						
							|  |  |  | interactive debugger for the error, rather than crashing the server.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | If a syntax error is already present when calling ``flask run``, it will
 | 
					
						
							|  |  |  | fail immediately and show the traceback rather than waiting until the
 | 
					
						
							|  |  |  | site is accessed. This is intended to make errors more visible initially
 | 
					
						
							|  |  |  | while still allowing the server to handle errors on reload.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | In Code
 | 
					
						
							|  |  |  | -------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 06:11:35 +08:00
										 |  |  | The development server can also be started from Python with the :meth:`Flask.run`
 | 
					
						
							|  |  |  | method. This method takes arguments similar to the CLI options to control the server.
 | 
					
						
							|  |  |  | The main difference from the CLI command is that the server will crash if there are
 | 
					
						
							|  |  |  | errors when reloading. ``debug=True`` can be passed to enable debug mode.
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 06:11:35 +08:00
										 |  |  | Place the call in a main block, otherwise it will interfere when trying to import and
 | 
					
						
							|  |  |  | run the application with a production server later.
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | .. code-block:: python
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  |     if __name__ == "__main__":
 | 
					
						
							|  |  |  |         app.run(debug=True)
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  | .. code-block:: text
 | 
					
						
							| 
									
										
										
										
											2014-04-28 19:27:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 05:46:00 +08:00
										 |  |  |     $ python hello.py
 |