Skip to content

Instantly share code, notes, and snippets.

@miladhzzzz
Created July 2, 2023 19:01
Show Gist options
  • Save miladhzzzz/e0cf7d450f4259177fe6042eedb2f9f8 to your computer and use it in GitHub Desktop.
Save miladhzzzz/e0cf7d450f4259177fe6042eedb2f9f8 to your computer and use it in GitHub Desktop.
Quick Host using ngrok!

Quick Host Ngrok Tool!

If you want to quickly host a directory on your machine over the internet this will help you!

Usage

# If you dont have / or are not sure if you ngrok use this command
python qhost.py /path/to/directory -i

# If You have Ngrok installed use it without -i
python qhost.py /path/to/directory

have fun!

import subprocess
import argparse
import sys
import os
def install_ngrok():
try:
subprocess.check_output(["ngrok", "--version"])
print("ngrok is already installed.")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print("ngrok is not found. Installing ngrok...")
try:
subprocess.check_output(["pip", "install", "ngrok"])
print("ngrok installed successfully.")
return True
except (subprocess.CalledProcessError, FileNotFoundError):
print("Failed to install ngrok. Please install it manually.")
return False
def start_ngrok(directory):
ngrok_process = subprocess.Popen(["ngrok", "http", "80"])
ngrok_ready = False
public_url = ""
while not ngrok_ready:
ngrok_output = subprocess.check_output(["curl", "http://localhost:4040/api/tunnels"])
ngrok_output = ngrok_output.decode("utf-8")
for line in ngrok_output.split("\n"):
if "public_url" in line:
public_url = line.split(": ")[1].strip('"')
ngrok_ready = True
break
print("Your directory is now hosted at:")
print(public_url)
try:
while True:
pass
except KeyboardInterrupt:
pass
ngrok_process.terminate()
def main():
parser = argparse.ArgumentParser(description="Host a directory with ngrok")
parser.add_argument("directory", help="Directory path to host")
parser.add_argument("-i", "--install", action="store_true", help="Install ngrok if not found")
args = parser.parse_args()
if args.install:
if not install_ngrok():
sys.exit(1)
if not os.path.isdir(args.directory):
print("Invalid directory path.")
sys.exit(1)
os.chdir(args.directory)
start_ngrok(args.directory)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment