What is the Python 3 equivalent of “python -m SimpleHTTPServer”

后端 未结 6 704
萌比男神i
萌比男神i 2020-11-28 00:19

What is the Python 3 equivalent of python -m SimpleHTTPServer?

6条回答
  •  醉梦人生
    2020-11-28 00:29

    In one of my projects I run tests against Python 2 and 3. For that I wrote a small script which starts a local server independently:

    $ python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")')
    Serving HTTP on 0.0.0.0 port 8000 ...
    

    As an alias:

    $ alias serve="python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")')"
    $ serve
    Serving HTTP on 0.0.0.0 port 8000 ...
    

    Please note that I control my Python version via conda environments, because of that I can use python instead of python3 for using Python 3.

提交回复
热议问题