有时候局域网共享个东西不方便,尤其在服务器上的时候,总不能先下载下来,再上传上去吧,于是经常在这台机器用python起个http服务,然后去另一台机器直接访问,一来二去,妥试不爽,特进行一下分离
python3
python -m http.server 80
Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
python2
python -m SimpleHTTPServer 80
Serving HTTP on 0.0.0.0 port 80 ...
如果是本机访问的话,可以直接在浏览器输入:http://0.0.0.0
那么问题来了,如果是想让其它机器访问怎么办?还要想办法得到自己ip
python -c "import socket;print([(s.connect((‘8.8.8.8‘, 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])"
192.168.2.10
所以两句合并的python3版才是我常用的
sudo python -m http.server 80 --bind `python -c "import socket;print([(s.connect((‘8.8.8.8‘, 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])"`
Serving HTTP on 192.168.2.100 port 80 (http://192.168.2.100:80/) ...
192.168.2.100 - - [18/Mar/2020 20:20:18] "GET / HTTP/1.1" 200 -
192.168.2.100 - - [18/Mar/2020 20:20:19] code 404, message File not found
192.168.2.100 - - [18/Mar/2020 20:20:19] "GET /favicon.ico HTTP/1.1" 404 -
that‘s all
原文地址:https://blog.51cto.com/rainbird/2480593
时间: 2024-11-02 23:17:44