LoginSignup
6
9

More than 5 years have passed since last update.

ヘッダの確認ができる簡易WEBサーバーを立ち上げる

Posted at

80 ポートで起動するWEBサーバーを立ち上げるには、
sudo python -m SimpleHTTPServer 80

と書けば一発で立ち上がり、そのディレクトリ上のファイルにアクセスが可能になる。
しかし、これではヘッダを入力した時にデバッグができないため、ヘッダが表示される簡易WEBサーバーにした。

printHeadersHttpServer.py
import SimpleHTTPServer
import SocketServer

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = ServerHandler
SocketServer.TCPServer(("", 80), Handler).serve_forever()

実行し、curlでアクセスする。
sudo python printHeadersHttpServer.py
curl http:localhost/peki.txt
コンソールに以下のようにヘッダも出力される。デバッグにクソ便利。

User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: localhost
Accept: */*

localhost.localdomain - - [07/Oct/2015 20:20:15] "GET /peki.txt HTTP/1.1" 200 -
6
9
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
9