python2 :
#ipv4
python -m SimpleHTTPServer 8080
python -m SimpleHTTPServer 8080 --bind 127.0.0.1
python -m SimpleHTTPServer 8080 --bind 0.0.0.0
#ipv6
python -c "import socket,SocketServer,CGIHTTPServer;SocketServer.TCPServer.address_family=socket.AF_INET6;CGIHTTPServer.test()" 8080
python3 :
https://docs.python.org/3/library/http.server.html
不指定端口的话默认为8000端口, 推荐使用80端口
# ipv4
python3 -m http.server 8080
python3 -m http.server 8080 --bind 127.0.0.1
python3 -m http.server 8080 --bind 0.0.0.0
#指定目录,需要python3.7以上版本,-d/--directory
python3 -m http.server 8080 --bind 0.0.0.0 -d /tmp/
补充 :
# 如果不指定目录的话, 你在哪个路径下执行的这个开启http服务的命令, 默认的物理路径就是当前路径
# 所以推荐做测试的时候, 可以单独新建一个文件夹, 这样可以减少信息泄露
使用utf-8编码:
避免出现中文乱码情况
webserver - How can I serve files with UTF-8 encoding using Python SimpleHTTPServer? - Stack Overflow
python2 :
python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();"
# 指定端口
python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();" 8080
# 指定端口和bind
python -c "import SimpleHTTPServer; m = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map; m[''] = 'text/plain'; m.update(dict([(k, v + ';charset=UTF-8') for k, v in m.items()])); SimpleHTTPServer.test();" 8080 --bind 0.0.0.0
python3:
#默认端口8000
python3 -c "from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH)"
# 指定端口
python3 -c "from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH)" 8080
# 指定端口和bind
python3 -c "from http.server import test, SimpleHTTPRequestHandler as RH; RH.extensions_map={k:v+';charset=UTF-8' for k,v in RH.extensions_map.items()}; test(RH)" 8080 --bind 0.0.0.0