LoginSignup
3
4

More than 5 years have passed since last update.

カレントディレクトリをドキュメントルートとしてHTTPサーバを立てる

Posted at
$python -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...
127.0.0.1 - - [17/Dec/2013 11:39:07] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Dec/2013 11:39:08] code 404, message File not found
127.0.0.1 - - [17/Dec/2013 11:39:08] "GET /favicon.ico HTTP/1.1" 404 -

デフォルトでは8000番ポートが開く。ポート番号を指定することもできる

$python -m SimpleHTTPServer 3000

Serving HTTP on 0.0.0.0 port 3000 ...
127.0.0.1 - - [17/Dec/2013 11:40:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Dec/2013 11:40:14] code 404, message File not found
127.0.0.1 - - [17/Dec/2013 11:40:14] "GET /favicon.ico HTTP/1.1" 404 -

なぜこんなことが可能なのかというと、SimpleHTTPServerというHTTPリクエストハンドラの機能を持つモジュールが提供されている。
参照: 20.19. SimpleHTTPServer — Simple HTTP request handler — Python v2.7.6 documentation

そして、"-m"オプションを付けることで、モジュールをスクリプトとして実行できる。

When called with -m module-name, the given module is located on the Python module path and executed as a script.
1. Command line and environment — Python v2.7.6 documentation

ちなみに、Python3からはhttp.serverにマージされたらしい

The SimpleHTTPServer module has been merged into http.server in Python 3.
20.19. SimpleHTTPServer — Simple HTTP request handler — Python v2.7.6 documentation

python -m http.server

これで起動できる。
参照:What is the python3 equivalent of "python -m SimpleHTTPServer" - Stack Overflow

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4