VOOZH about

URL: https://qiita.com/shigenobu_c/items/9fc9a0c97a7acd5a51f7

⇱ python で one liner webServer(with CGI) #Python - Qiita


👁 Image
3

Go to list of users who liked

3

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@shigenobu_c

python で one liner webServer(with CGI)

3
Last updated at Posted at 2014-12-03

やりたいこと

簡易webServerをつくって自分のアプリの動作確認をしたい。
(DummyServer的なものが欲しい)

HowTo

何をつかう?

やりかたはいろいろありそうでしたが、pythonが手っ取り早そうなのでpythonを採用
(手っ取り早い=>linuxに標準で入っている)

いろいろ = ncコマンド とか、php,ruby,などなどでも可能。

やってみる

アクセス先を作成

(どこでもいいけどとりあえずtmpでやってみた)
cd /tmp 

(今回はcgiアクセスしたいので以下のdirを作成(デフォだとcgi-binかhtbinという名称じゃないとダメっぽい))
mkdir cgi-bin

(なんかスクリプトを配置してみる)

vi hoge.py

中身は↓↓↓

cat hoge.py 
# !/usr/bin/env python
# -*- coding: utf-8 -*-

hoge="Welcome to one liner."

print """Content-Type: text/html

<html>
<body>
<h2>{0}</h2>
</body>
</html>
""".format(hoge)

実行権限を与えてあげる 👁 :fist:

$ chmod 655 hoge.py

webServerを起動

$ sudo python -m CGIHTTPServer 1234
Serving HTTP on 0.0.0.0 port 1234 ...

アクセス! 👁 :punch:

$ curl localhost:1234/cgi-bin/hoge.py
<html>
<body>
<h2>Welcome to one liner.</h2>
</body>
</html>


アクセスログもでます 

$ sudo python -m CGIHTTPServer 1234
Serving HTTP on 0.0.0.0 port 1234 ...
localhost - - [03/Dec/2014 19:06:01] "GET / HTTP/1.1" 200 -
localhost - - [03/Dec/2014 19:06:06] "GET /cgi-bin/hoge.py HTTP/1.1" 200 -

まとめ 👁 :sparkles:

ものすごく簡単じゃないですか!?
社内でファイルサーバー立てるのがめんどいとか、
冒頭で書きましたがDummyServer(stub)的な使い方とかとりあえず、なんか動かしたいときははかなり
簡単に利用できるんじゃないでしょうか!

3

Go to list of users who liked

3
0

Go to list of comments

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

Go to list of users who liked

3