開発サーバ Development Server

Flaskはアプリケーションを開発サーバを使って走らせるrunコマンドを提供します。デバッグモードでは、このサーバはインタラクティブなデバッガを提供し、コードが変更されたときは再読み込み(reload)します。 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.

警告

本番環境へ展開(deploy)するときは、開発サーバは使わないでください。それはローカルでの開発の間だけ使うために意図されたものです。それは効率性、安定性、セキュリティを特別意識して設計されてはいません。 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.

展開(deploy)のオプションについては本番環境への展開(Deployment to Production)を見てください。 See :doc:`/deploying/index` for deployment options.

コマンドライン Command Line

CLIのコマンドであるflask runは、開発サーバを走らせる推奨される方法です。自分のアプリケーションを指すために--appオプションを、デバッグモードを有効にするために--debugオプションを使ってください。 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.

$ flask --app hello --debug run

この例ではインタラクティブなデバッガと再読み込み機能(reloader)を含む、デバッグモードを有効にし、それからhttp://localhost:5000/でサーバを開始します。利用可能なオプションを見るにはflask run --helpを使用し、CLIの使用や設定についての指示(instructions)の詳細はコマンドライン・インタフェースを見てください。 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.

Address already in use

もし他のプログラムが既に5000番ポートを使用中であるときは、サーバを開始しようと試みたときにOSErrorを見ることになるでしょう。それは以下のメッセージの中の1つでしょう。 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

他のプログラムを特定して停止するか、flask run --port 5001を使って違うポートを選んでください。 Either identify and stop the other program, or use ``flask run --port 5001`` to pick a different port.

ポートを使っているのがどのプロセスIDなのか特定するためにnetstatまたはlsofを使うことができ、それからOSの他のツールを使ってそのプロセスを止めてください。以下の例は、プロセスIDが6847のプロセスが5000番ポートを使っていることを示しています。 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.

$ netstat -nlp | grep 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 6847/python

macOS Monterey以降は、自動的に5000番ポートを使うサービスを開始します。そのサービスを無効にするには、System Preferences、Sharingへ行き、「AirPlay Receiver」を無効にします。 macOS Monterey and later automatically starts a service that uses port 5000. To disable the service, go to System Preferences, Sharing, and disable "AirPlay Receiver".

再読み込み時のエラーの遅延処理(Deferred Errors on Reload) Deferred Errors on Reload

再読み込み機能(reloader)と一緒にflask runコマンドを使うとき、もし文法エラーまたはその他の初期化エラーをコードに導入してしまった場合でも、そのサーバーは走り続けようとします。サイトへアクセスすると、サーバがクラッシュするのではなく、エラーに対するインタラクティブなデバッガを見られるでしょう。 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.

もしflask runを呼んだとき文法エラーが既に存在している場合、サイトにアクセスされるまで待つのではなく、flask runコマンドは即座に失敗してトレースバックを表示します。サーバーがreload時はエラーを処理できるようにしながら、最初はエラーをより可視化させるために、これは意図されたものです。 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.

コードの中 In Code

開発サーバはPythonからもFlask.run()メソッドを使って開始できます。このメソッドはサーバーを制御するためにCLIのオプションと似た引数を取ります。CLIコマンドからの主な違いは、reload時にエラーがあればクラッシュすることです。デバッグモードを有効にするためにdebug=Trueを(Flask.run()に)渡すことができます。 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.

mainのブロック内にFlask.runの呼び出しを置いてください。そうしないと、後で本番環境のサーバー内でアプリケーションをimportして走らせようとするときに、その呼び出しが邪魔をするでしょう。 Place the call in a main block, otherwise it will interfere when trying to import and run the application with a production server later.

if __name__ == "__main__":
    app.run(debug=True)
$ python hello.py