Setup a Static IP for Python HTTPS using HTTPX

Learn how to route Python HTTP traffic through a QuotaGuard Static IP using the HTTPX library.

Prerequisites

N/A

Instructions

Run example
QUOTAGUARDSTATIC_URL=... python app.py

Be sure to set QUOTAGUARDSTATIC_URL to your Connection URL from the QuotaGuard Dashboard.

Test in Docker
docker build -t qg-static-python-https-httpx-example .
docker run -e QUOTAGUARDSTATIC_URL=... qg-static-python-https-httpx-example

Code Samples

app.py
import os
import httpx

QUOTAGUARD_URL = os.getenv("QUOTAGUARDSTATIC_URL", "")
TEST_URL = "https://ip.quotaguard.com/"

if not QUOTAGUARD_URL:
    print("Error: QUOTAGUARD_URL environment variable not set.")
    exit(1)

try:
    response = httpx.get(TEST_URL, proxy=QUOTAGUARD_URL)
    print("Response from QuotaGuard:")
    print(response.text)
except Exception as e:
    print("Request failed:")
    print(e)

Docker File
FROM python:latest

WORKDIR /app

# Install dependencies
RUN pip install httpx

# Copy app code
COPY app.py .

# Set the default command
CMD ["python", "app.py"]