使用python2与python3创建简单的http服务
python2基于SimpleHTTPServer创建http服务
首先,在linux服务器上面检查一下自己的python版本;如:
|
1
2
|
[root@tx_db1 ~]# python -V
Python 2.7.13
|
一、在2.7的环境下,使用如下命令;
|
1
2
|
[root@tx_db1 acc]# python -m SimpleHTTPServer 8087Serving HTTP on 0.0.0.0 port 8087 ... |
后面8887端口是手动指定的。默认为8080; 这样就启动了一个http的服务,目前是监听状态;再打开一个窗口进行访问下;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@zabbix ~]# curl -I http://127.0.0.1:8087HTTP/1.0 200 OKServer: SimpleHTTP/0.6 Python/2.7.13Date: Tue, 21 May 2019 10:32:37 GMTContent-type: text/html; charset=UTF-8Content-Length: 326再回到第一个窗口看一下监听状态;已经有访问了;[root@tx_db1 acc]# python -m SimpleHTTPServer 8087Serving HTTP on 0.0.0.0 port 8087 ...
127.0.0.1 - - [19/Mar/2020 15:33:43] "HEAD / HTTP/1.1" 200 -
|
这也是在windows 系统的浏览器下输入地址:0.0.0.0:/awsm_input.txt(0.0.0.0为服务器ip) ,就能下载acc目录下的awsm_input.txt文件了,并且查看监听状态显示为:
[root@tx_db1 acc]# python -m SimpleHTTPServer 8087
Serving HTTP on 0.0.0.0 port 8087 ...
127.0.0.1 - - [19/Mar/2020 15:33:43] "HEAD / HTTP/1.1" 200 -
10.0.0.163 - - [19/Mar/2020 15:34:55] "GET /anwd_input_litter.txt HTTP/1.1" 200 -
二、在python3的环境下,创建http服务:
|
1
2
|
[root@zabbix flask]# python -VPython 3.6.6 |
现在我使用python3.6版本来创建http服务;python3.6中要使用http.server来创建。与python2略微不同;
|
1
2
|
[root@zabbix flask]# python -m http.server 8880Serving HTTP on 0.0.0.0 port 8880 (http://0.0.0.0:8880/) ... |
访问可以参照python2。
浙公网安备 33010602011771号