$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