QuotaGuard Heroku Django and Azure SQL DB

import pandas as pd
from PIL import Image
from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
from dotenv import load_dotenv
import socks
import socket
import os
import io

load_dotenv()

CLIENT_ID = os.getenv("CLIENT_ID")
TENANT_ID = os.getenv("TENANT_ID")
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
DB_SERVER = os.getenv("DB_SERVER")
DB_NAME = os.getenv("DB_NAME")
DB_DRIVER = os.getenv("DB_DRIVER")
DB_TIMEOUT = os.getenv("DB_TIMEOUT")
QUOTAGUARD_URL = os.getenv("QUOTAGUARD_URL")

if QUOTAGUARD_URL:
    proxy_protocol, proxy_auth_host = QUOTAGUARD_URL.split("://")
    proxy_auth, proxy_host_port = proxy_auth_host.split("@")
    proxy_user, proxy_pass = proxy_auth.split(":")
    proxy_host, proxy_port = proxy_host_port.split(":")
    proxy_port = int(proxy_port)

socks.set_default_proxy(socks.SOCKS5, proxy_host, proxy_port, username=proxy_user, password=proxy_pass)
socket.socket = socks.socksocket

connection_string = (
    "mssql+pyodbc:///?odbc_connect=" +
    f"Driver={{{DB_DRIVER}}};" +
    f"Server={DB_SERVER};" +
    f"Database={DB_NAME};" +
    "Encrypt=yes;" +
    "TrustServerCertificate=yes;" +
    f"Connection Timeout={DB_TIMEOUT};" +
    "Authentication=ActiveDirectoryServicePrincipal;" +
    f"UID={CLIENT_ID};" +
    f"PWD={CLIENT_SECRET};" +
    f"Authority Id={TENANT_ID};"
)

engine = create_engine(connection_string, poolclass=NullPool)

try:
    query = "SELECT * FROM list_photos"
    with engine.connect() as conn:
        df = pd.read_sql(query, conn)
        print("DataFrame loaded successfully!")
        print(df.head())
except Exception as e:
    print(f"Error: {e}")

for index, row in df.iterrows():
    holdon = input("Press Enter to view the next image...")
    try:
        image = Image.open(io.BytesIO(row['photo']))
        image.show()
    except Exception as e:
        print(f"Error displaying image at index {index}: {e}")

I'm trying to pull the data from Azure SQL database via proxy but it is not using this proxy to connect to it therefore I'm getting an error from database where it says data source name not found etc etc etc which is typical when I'm trying to connect to DB via IP which is not in whitelist. The proxy's IP is in whitelist that's how I know it is not using proxy to make a request plust in QuotaGuard dashboard I don't see this request as well.

I've read all the documentation here: https://devcenter.heroku.com/articles/quotaguard#testing-in-the-python-interpreter

Also I've tried different methods of connecting, what am I doing wrong?

Вернуться на верх