LoginSignup
6
5

One Liner Web Server

Last updated at Posted at 2016-09-28

ダウンロード速度の計測目的などで、webサーバを簡易的、一時的に起動させたい。
でも、apacheなどをインストールしてlibやbinにproductionとは関係ないファイルが残るのは嫌だなぁ。
そんな時、pythonでwebサーバを起動させる(httpをLISTENさせる)ことができます。しかも one liner で!

サーバ側

1. rootになります
2. カレントディレクトリにファイルを作成します。これがhttpで配信するファイルになります

# echo hoge > index.html

3. 次を実行して80ポートをLISTEN状態にする(webサーバを起動する)

# python -m SimpleHTTPServer 80
※80を指定しなければデフォルトのLISTEN PORTは8000です

<2024-03-25追記>
python3ではSimpleHTTPServerモジュールがhttp.serverモジュールにリプレイスされています。

# python -m http.server 80
※80を指定しなければデフォルトのLISTEN PORTは8000です

</2024-03-25追記>

クライアント側

1. curlなりwgetなりでindex.htmlを取得する

$ wget http://サーバのホスト名/index.html

2. ファイルを確認

$ cat index.html
$ hoge

参考URL

http://d.hatena.ne.jp/rx7/20090812/p1

6
5
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
6
5