Skip to content

Instantly share code, notes, and snippets.

@omar2205
Last active July 24, 2022 08:39
Show Gist options
  • Save omar2205/c8ce5850220c6fe926b49e30d229b5d1 to your computer and use it in GitHub Desktop.
Save omar2205/c8ce5850220c6fe926b49e30d229b5d1 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
# Quickly host a static server using python3
# Usage:
# -p <PORT>
# -d <DIR> the directory to serve
# -i <IP> the ip to bind to
# defaults values
PORT=3000
DIR=. # current dir
IP="localhost"
usage() {
echo "Usage: $0 -p <PORT> -d <DIR> -i <IP>"
}
while getopts ":hp:d:i:" arg; do
case $arg in
p)
PORT=${OPTARG}
;;
d)
DIR=${OPTARG}
;;
i)
IP=${OPTARG}
;;
h | *)
usage
exit 1
;;
esac
done
echo "Starting server on [$IP:$PORT]"
echo " Dir:$DIR ($(pwd))"
python3 -m http.server "$PORT" --directory "$DIR" --bind "$IP"
import os
from bottle import route, request, run
PORT = os.environ.get('PORT', 4000)
IP = os.environ.get('MY_IP', False)
USER = os.environ.get('USER', 'ubuntu')
if not IP:
import requests
r = requests.get('http://checkip.amazonaws.com')
IP = r.text.rstrip()
@route('/give', method=['POST', 'PUT'])
def give():
file = request.files.get('file')
print(f'Got -> {file.filename}')
file.save(f'/home/{USER}/dls/', overwrite=True)
RED='\033[0;31m'
GRN='\033[1;32m'
BLD='\033[0;1m'
ULN='\033[0;4m'
BLINK='\033[0;5m'
NC='\033[0m' # Reset
print(f'{BLINK}> {NC}{GRN}Use {BLD}curl -X POST -F file=@<FILE> {IP}:{PORT}/give{NC}\n')
run(host='0.0.0.0', port=PORT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment