Last active
October 16, 2020 21:51
-
-
Save antiboredom/f6674cbe8efd84dc82d13c9821789a10 to your computer and use it in GitHub Desktop.
Create a static http server on an open port between 8000 and 9000. Just add to your bash_profile...
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
# stick this in your .bash_profile to create | |
# a static http server on an open port (btw 8000 and 9000) | |
# should work in mac and linux | |
function serve() { | |
# find an open port, from https://unix.stackexchange.com/a/358101 | |
local openport=$(netstat -aln | awk ' | |
$6 == "LISTEN" { | |
if ($4 ~ "[.:][0-9]+$") { | |
split($4, a, /[:.]/); | |
port = a[length(a)]; | |
p[port] = 1 | |
} | |
} | |
END { | |
for (i = 8000; i < 9000 && p[i]; i++){}; | |
if (i == 9000) {exit 1}; | |
print i | |
} | |
') | |
python3 -m http.server $openport | |
# alternatively use just this if you don't | |
# need automatic port finding | |
# python3 -m http.server ${1-8000}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
paste the above in your
.bash_profile
and then use the commandserve
to start an http server.