zoukankan      html  css  js  c++  java
  • 使用 python快速搭建http服务

    在 Linux 服务器上或安装了 Python 的机器上,Python自带了一个WEB服务器 SimpleHTTPServer。

    我们可以很简单的使用  python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web服务。

    命令如下:

     python3 -m http.server

    python -m SimpleHTTPServer 8000

    使用上面的命令可以把当前目录发布到8000端口。

    但是这条命令是当前运行的,不是后台运行的,也就是说如果Ctrl + C,则该端口就会关闭。

    python -m SimpleHTTPServer 8000 &

    在上述命令的最后加一个 & ,则该命令产生的进程在后台运行,不会影响当前终端的使用(我们在只有一个bash的环境下)。

    生成的新的进程为当前bash的子进程,所以,当我们关闭当前bash时,相应的子进程也会被kill掉,这也不是我们想要的结果。

    nohup python -m SimpleHTTPServer 8000 &

    在命令的开头加一个nohup,忽略所有的挂断信号,如果当前bash关闭,则当前进程会挂载到init进程下,成为其子进程,这样即使退出当前用户,其8000端口也可以使用。

    对于不同的python版本有不同的方式,下面就一一介绍。

    Python <= 2.3

    python -c "import SimpleHTTPServer as s; s.test();" 8000
    

    Python >= 2.4

    python -m SimpleHTTPServer 8000
    

    Python 3.x

    python -m http.server 8000
  • 相关阅读:
    #Leetcode# 451. Sort Characters By Frequency
    #Leetcode# 148. Sort List
    PAT 甲级 1138 Postorder Traversal
    PAT 甲级 1141 PAT Ranking of Institutions
    PAT 甲级 1142 Maximal Clique
    PAT 甲级 1146 Topological Order
    PAT 甲级 1143 Lowest Common Ancestor
    #Leetcode# 347. Top K Frequent Elements
    牛客寒假算法基础集训营4
    PAT 1147 Heaps
  • 原文地址:https://www.cnblogs.com/dggsec/p/9219282.html
Copyright © 2011-2022 走看看