Skip to main content
ngrok logo

Download ngrok

ngrok is your app’s front door—and the fastest way to put anything on the internet.

SDK

Python

Installation

Setup an example by making a directory:

mkdir hello-ngrok

Then, change to that directory:

cd hello-ngrok

Install python on your system here or use Homebrew:

brew install python

Install the python package:

python3 -m pip install ngrok

Now we need a runnable example. Let's create a new entry file:

touch example.py

Put your app online

hello-ngrok/example.py

#!/usr/bin/env python

from http.server import HTTPServer, BaseHTTPRequestHandler
import logging, ngrok


class HelloHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        body = bytes("Hello", "utf-8")
        self.protocol_version = "HTTP/1.1"
        self.send_response(200)
        self.send_header("Content-Length", len(body))
        self.end_headers()
        self.wfile.write(body)

logging.basicConfig(level=logging.INFO)

# Create the server and attach ngrok
server = HTTPServer(("localhost", 0), HelloHandler)
ngrok.listen(server)

try:
    logging.info("Starting server. Press Ctrl+C to stop.")
    server.serve_forever()
except KeyboardInterrupt:
    logging.info("Shutting down server...")
    server.server_close()
    ngrok.kill()
    logging.info("Server stopped cleanly.")

Run your Python app with your ngrok authtoken as an environment variable. for a free account to get your authtoken.

NGROK_AUTHTOKEN= python3 example.py