Last active
February 12, 2020 14:51
-
-
Save mogeko/7bf5a21700868a9c7b1243baa165e8d0 to your computer and use it in GitHub Desktop.
一个简单的本地 HTTP 服务器,用于在局域网中分享文件。python3 -m http.server 的升级版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:' | while read ip; do python3 -m http.server 8000 --bind $ip; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
众所周知,在 shell 中执行
python3 -m http.server
可以很轻松的创建一个 HTTP 服务器来分享文件。但是默认情况下
python
会以http://0.0.0.0:8000
为地址创建服务器。此时,局域网中用户是不能直接访问http://0.0.0.0:8000
的,而应该访问http://[分享者的 IP]:8000
。但是用户往往不清楚分享者的 (局域网) IP 地址是多少于是我改进了这一命令,改为:
改进后,会自动以
http://[分享者的 IP]:8000
为地址创建服务器,并显示到屏幕上依赖
使用
在终端中执行:
应该没人想记这么长的命令吧?我们可以使用脚本来帮助我们
我原本准备用
alias
将此命令设置成别名,但似乎用不了不过我们可以曲线救国,首先创建一个脚本文件 :
HTTPServer
(或者下载上面那个文件),赋予可执行的权限,然后将脚本文件放到/usr/local/bin/
目录下。在任意位置执行
HTTPServer
即可使用