凉城旧巷
Python从入门到自闭,Java从自闭到放弃,数据库从删库到跑路,Linux从rm -rf到完犊子!!!

Linux上使用SimpleHTTPServer 快速搭建http服务

一、SimpleHTTPServer介绍

在 Linux 服务器上或安装了 Python 的机器上,可以使用 python -m SimpleHTTPServer [port] 快速搭建一个http服务。

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

使用 python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web服务,实现文件共享。

 

二、搭建SimpleHTTPServer

1、master机进入需共享目录

cd xxx

2、master机启动web服务器

# python -m SimpleHTTPServer [port]       # 不指定端口默认使用 8000端口
python -m SimpleHTTPServer 8000

3、master机防火墙开放端口

关闭防火墙,或者开放8000端口,否则会导致无法通过ip:port访问

sudo /sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCEPT

4、slave机上浏览文件

浏览器中通过http://IP:Port即可访问共享目录下的文件

 

其他操作

  • 在命令最后加一个 & ,则该命令产生的进程在后台运行,不会影响当前终端的使用。生成的新的进程为当前bash的子进程,所以,当我们关闭当前bash时,相应的子进程也会被kill

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

    nohup python -m SimpleHTTPServer 8000 &
    

 

三、Python2 与Python3搭建http服务

1、Python2中搭建

SimpleHTTPServer是Python2自带的一个模块

python -m SimpleHTTPServer [port]

 

2、Python3中搭建

SimpleHTTPServer在Python3中封装在了http.server模块中

python3 -m http.server [port]

 

四、下载文件

1、通过浏览器访问http://ip:port,然后直接下载

2、通过wget下载

wget http://ip:port/[目录下的文件]
# wget http://10.1.1.1:9999/test.txt
posted on 2020-12-03 15:38  凉城旧巷  阅读(3799)  评论(0编辑  收藏  举报