-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
40 lines (34 loc) · 1.06 KB
/
Copy pathapp2.py
File metadata and controls
40 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import pyodbc
from flask import Flask
from sqlalchemy import create_engine
import requests
app = Flask(__name__)
# Get the Managed Identity token
token_response = requests.get(
"http://169.254.169.254/metadata/identity/oauth2/token",
params={
"api-version": "2019-08-01",
"resource": "https://database.windows.net/"
},
headers={"Metadata": "true"}
)
access_token = token_response.json()['access_token']
# Connection string with access token
connection_string = (
"Driver={ODBC Driver 17 for SQL Server};"
"Server=tcp:hawkeye-server-test.database.windows.net,1433;"
"Database=hawkeye-DB-test;"
"Authentication=ActiveDirectoryMsi;"
f"AccessToken={access_token};"
"Encrypt=yes;"
"TrustServerCertificate=no;"
"Connection Timeout=30;"
)
# Create engine with connection string
engine = create_engine(f"mssql+pyodbc:///?odbc_connect={connection_string}")
@app.route('/')
def hello():
with engine.connect() as conn:
result = conn.execute("SELECT @@version")
return str(result.fetchone())