stylesheet

2018-09-01

ちょっとした確認用にpythonでhttpサーバーを起動

Pythonモジュールの実行で済むわけですが...

Python2.x
$ python -m SimpleHTTPServer
Python3.x
$ python3 -m http.server

これすら覚えられずに毎回検索しているわけで...

$ touch ~/.local/bin/httpserv
$ chmod +x ~/.local/bin/httpserv
$ $EDITOR ~/.local/bin/httpserv
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Usage: httpserv [PORT=8000]

import sys
try:
    # py2
    import SimpleHTTPServer
    SimpleHTTPServer.test()
except ImportError:
    # py3
    import http.server
    if sys.argv[1:]:
        port = int(sys.argv[1])
    else:
        port = 8000
    http.server.test(port=port)

コマンド化すると補間も効くので大丈夫だろうと言うことです。