Skip to content

Instantly share code, notes, and snippets.

@danieldsf
Last active August 17, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldsf/65a6c39c03924bc884d2e38bc61f3c0b to your computer and use it in GitHub Desktop.
Save danieldsf/65a6c39c03924bc884d2e38bc61f3c0b to your computer and use it in GitHub Desktop.
servecli.py
import socket, click, os, socketserver
from http.server import SimpleHTTPRequestHandler
from pathlib import Path
ATTEMPT_HOST = '10.255.255.255'
HOST = ('0.0.0.0', 8080)
def getip() -> str:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect((ATTEMPT_HOST, 1))
IP = str(s.getsockname()[0])
except Exception:
IP: str = '127.0.0.1'
finally:
s.close()
return IP
@click.command()
@click.option("--path", prompt="Your path", default='.', help="Path.")
def index(path: str = '.'):
my_ip = getip()
resolved_path = str(Path(os.curdir, path).resolve())
"""Simple program that gets a path and serve its files in a specific path."""
try:
if os.path.isdir(resolved_path):
os.chdir(resolved_path)
click.secho(f"Current path {resolved_path} is listed at http://{my_ip}:{HOST[1]}", fg='green')
httpd = socketserver.TCPServer(HOST, SimpleHTTPRequestHandler)
httpd.serve_forever()
else:
click.secho('Directory not found', fg='red')
except Exception as e:
click.secho(str(e), fg='red')
if __name__ == '__main__':
index()
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
invoke = "*"
click = "*"
[dev-packages]
[requires]
python_version = "3.8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment