Skip to content

Instantly share code, notes, and snippets.

@antiboredom
Last active October 16, 2020 21:51
Show Gist options
  • Save antiboredom/f6674cbe8efd84dc82d13c9821789a10 to your computer and use it in GitHub Desktop.
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...
# 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};
}
@antiboredom
Copy link
Author

paste the above in your .bash_profile and then use the command serve to start an http server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment