【Python 开发】第三篇:python 实用小工具

一、快速启动一个web下载服务器

1)web服务器:使用SimpleHTTPServer,快速帮我们共享指定目录下内容。

  • 各种Linux发行版通常都内置了Python,故使用此方法非常方便。在其它OS(比如Windows)此方法也有效,但是要麻烦一些,必须先搭建Python环境。
  • SimpleHTTPServer是Python 2自带的一个模块,是Python的Web服务器。它在Python 3已经合并到http.server模块中。

SimpleHTTPServer有一个特性:

  • 如果待共享的目录下有index.html,那么index.html文件会被视为默认主页;
  • 如果不存在index.html文件,那么就会显示整个目录列表。

2)SimpleHTTPServer使用方法:

  1)进入待分享的目录
  2)执行命令python -m SimpleHTTPServer 端口号
   注意:不填端口号则默认使用8000端口。
  3)浏览器访问该主机的地址:http://IP:端口号/

示例:执行命令

在python2.x中:
[root@host130 ~]# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.31.86 - - [18/Sep/2018 21:17:53] "GET / HTTP/1.1" 200 -

也可以指定某个端口;
[root@yanglt tmp]# python -m SimpleHTTPServer 80
Serving HTTP on 0.0.0.0 port 80 ...
118.199.86.147 - - [18/Sep/2018 21:43:25] "GET / HTTP/1.1" 200 -

在Python3.x中:
[root@host130 ~]# python -m http.server  
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
192.168.31.86 - - [18/Sep/2018 21:19:15] "GET / HTTP/1.1" 200 -

该下载服务器,默认端口号是8000
打开网页输入:http://192.168.31.77:8000

猜你喜欢

转载自www.cnblogs.com/yangleitao/p/9671585.html