Need help connecting a Python Flask app to on-prem SQL Server

Noel Pena 20 Reputation points
2025-03-17T22:46:45.09+00:00

Hello,

I need some help with connecting my Python Flask API to an on-prem SQL Server. The Flask API that is hosted on Azure app service. And I have 2 other APIs on Azure app service that can successfully connect to the on-prem SQL server. This is possible through the Virtual network we setup. The other 2 apps are .NET apps on windows and this one is python Flask app on Linux.

I'm hoping someone has run into this before and can help.

The Flask API works fine except when I call an endpoint that requires a connection to my on-prem SQL Server. I receive this error:

2025-03-07T17:14:29.8834971Z sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08001', '[08001] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)') 

2025-03-07T17:14:29.8834988Z (Background on this error at: https://sqlalche.me/e/20/e3q8)

or I get the following:

2025-03-17T22:38:32.6484166Z pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)')`

`2025-03-17T22:38:32.6790697Z  Connection failed: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]TCP Provider: Error code 0x2746 (10054) (SQLDriverConnect)')`

When troubleshooting.

  • I can tcpping the on-prem SQL Server's IP address with no issue.
  • SQL Server is version 2016
  • I confirmed that the SQL server is using port 1433.
  • I made sure the SQL Server accepts SQL auth
  • I made sure the connection string is correct. I also tried Encrypt=yes and Encrypt=no. TrustServerCertificate=yes and TrustServerCertificate=no.
  • The SSL cert on the SQL server is self-signed so TrustServerCertificate=yes is usually set.
  • I've tried using the Connection string normally in my app and also using SQLAlchemy.
  • I have tried ODBC driver 17 and 18.
  • The SQL server works with both locally and with the other 2 apps on Azure app service using the same connection string
  • I tried modifying the odbc.ini and odbcinst.ini files
  • Confirmed my App in azure is connected to the virtual network
  • I confirmed that there is no Firewall issues with the connection from the app to the SQL Server
  • Confirmed remote access is allowed on the SQL Server

The issue seems to be related to the ODBC Driver, from what I can tell. The last couple of things I have been trying is to modify the /home/startup.sh script to update the ODBC 18 driver and make sure it's being used but I keep getting a driver registration error when trying the startup script

Any help is greatly appreciated please!

Thank you,

Noel

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
0 comments No comments
{count} votes

Accepted answer
  1. Laxman Reddy Revuri 5,395 Reputation points Microsoft External Staff Moderator
    2025-03-20T03:50:10.5633333+00:00

    Hi @Noel Pena
    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer

    Ask:Need help connecting a Python Flask app to on-prem SQL Server
    Solution:
    ere is what I was able to do to fix this issue. I thought it was an ODBC driver issue but it wasn't.

    I asked my admin to look at the event viewer on the SQL Server to see if there was anything suspicious and he found this error that corresponded with my attempts.

    Here's the error:

    plaintextCopy

    An TLS 1.2 connection request was received from a remote client application, but none of the cipher suites supported by the client application are supported by the server. The SSL connection request has failed.
    

    I created a startup.txt file in the root of my project and added the following:

    plaintextCopy

    #!/bin/bash
    echo "Starting startup script..."
    
    # Install OpenSSL and libssl-dev to support more cipher suites
    echo "Installing OpenSSL and libssl-dev..."
    apt-get install -y openssl libssl-dev
    
    # Update OpenSSL settings to lower SECLEVEL for ODBC 18
    echo "Updating /etc/ssl/openssl.cnf to support SQL Server's cipher suites..."
    sed -i 's/SECLEVEL=2/SECLEVEL=1/g' /etc/ssl/openssl.cnf
    
    # Start the application
    echo "Starting Gunicorn..."
    gunicorn --bind=0.0.0.0 --timeout 300 app:app
    

    These 2 things installing openssl and libssl-dev and updating the security level allowed me my Azure web app to connect with the on-prem SQL Server.

    Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members. 

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Laxman Reddy Revuri 5,395 Reputation points Microsoft External Staff Moderator
    2025-03-18T05:33:56.76+00:00

    Hi @Noel Pena
    It appears you are facing connectivity issues between your Python Flask app hosted on Azure App Service and you're on-prem SQL Server.

    Ensure the ODBC driver is correctly installed and registered. You mentioned modifying the /home/startup.sh script to update the ODBC driver. Verify that the driver is compatible with your Python environment and that the correct version is used.

    Double-check your connection string format. It should look like: Ensure that and are correctly filled in.  

     Driver={ODBC Driver 18 for SQL Server};Server=,1433;Database=;Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=30;
    
    

    Since you have verified that your app is connected to the virtual network, please also check that the network security groups (NSGs) and any firewall settings allow traffic from your Azure App Service to the on-prem SQL Server.
    As you are using a self-signed certificate, please ensure that it is trusted by the Azure environment

    Enable ODBC tracing to get more detailed logs about the connection attempts. This can help identify if the issue is with the driver or the connection itself.
    Kindly refer below documentation :

    Troubleshooting connection problems
    https://learn.microsoft.com/en-us/azure/azure-sql/database/azure-sql-python-quickstart?view=azuresql&tabs=windows%2Csql-inter#connect-the-app-service-to-azure-sql-database


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.