Last active
August 17, 2021 15:34
-
-
Save danieldsf/65a6c39c03924bc884d2e38bc61f3c0b to your computer and use it in GitHub Desktop.
servecli.py
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
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() | |
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
[[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