24

非常简单的Python3 HTTP服务

 5 years ago
source link: https://acuario.xyz/create-simple-http-server-with-python3/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

多年前在左耳朵耗子的博客上看到 《非常简单的 PYTHON HTTP 服务》 一文,在急需建立 HTTP Server 的场合十分有用,比如临时分享文件之类。在 HTTP Server 访问的根目录下只需执行一行命令就能搞定:

$ python -m SimpleHTTPServer

但是随着 Python 2 逐渐被 Python 3 取代,上述命令无法继续使用。在 Python3 中没有 SimpleHTTPServer ,而是直接使用 http.server 即可。所以对应的 Python 3 命令是:

$ python3 -m http.server

默认开启的 HTTP Server 服务监听的是 8000 端口,使用时注意系统防火墙是否放行。如需使用其他端口,只需在命令末尾加上端口号即可,如使用端口 1234:

$ python3 -m http.server 1234

后台运行

上述 Python 运行的 HTTP 服务器必须前台运行命令,并实时输出 log,断开终端后自动停止服务。这时可以借助 nohup 命令使其后台运行:

$ nohup python3 -m http.server >>/dev/null &

当然你也可以使用其他方法如 screen 等工具实现后台运行,再此就不赘述了。不过既然有更复杂的需要,那还是老老实实用 Nginx 吧。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK