python - pyodbc: How to test whether it's possible to establish connection with SQL server without freezing up -


i writing app wxpython incorporates pyodbc access sql server. user must first establish vpn connection before can establish connection sql server. in cases user forgets establish vpn connection or not authorized access particular server, app freeze 60+ seconds before produces error message. often, users impatient , force-close app before error message pops up.

i wonder if there way test whether it's possible connect server without freezing up. thought using timeout, seems timeout can used after establish connection

a sample connection string use below:

connection = pyodbc.connect(r'driver={sql server};server=servername;database=databasename;trusted_connection=true;unicode_results=true') 

see https://code.google.com/archive/p/pyodbc/wikis/connection.wiki under timeout

note: attribute affects queries. set timeout actual connection process, use timeout keyword of pyodbc.connect function.

so change connection string to:

connection = pyodbc.connect(r'driver={sql server};server=servername;database=databasename;trusted_connection=true;unicode_results=true', timeout=3)  

should work


Comments