简体   繁体   中英

Flask app fails to render when deploying code to Azure Web with a database connection, but works fine from local server

I have an Azure web app with a Flask template. I want it to connect to a SQL database. I made one. I installed pymssql. For testing purposes I added to the views.py in the root folder:

import pymssql
conn = pymssql.connect(server='mydb.database.windows.net', user='mydbnameadmin@mydb', password='secret', database='mydb')

I first tested locally on my kubuntu box. To make sure I was getting a connection, I entered the wrong password, and the server threw an error. I entered it correctly and the error disappeared. Cool. Also the SQL dashboard on the azure portal reported sucessfull connections. So then I pushed my changes (including updating the requirements.txt) to my github repo and it was sucked into my web application. When I tried running the web app, on the index page:

The page cannot be displayed because an internal server error has occurred.

After turning on and inspecting the detailed logs all I get it:

HTTP Error 500.0 - Internal Server Error
The page cannot be displayed because an internal server error has occurred.
Most likely causes:

IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
IIS was not able to process configuration for the Web site or application.
The authenticated user does not have permission to use this DLL.
The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.

IIS. What's that doing here? I thought MS was trendy and hosted this service on a linux box 0_0

I remove the second line

conn = pymssql.connect(server='mydb.database.windows.net', user='mydbnameadmin@mydb', password='secret', database='mydb')

and the error disappeared. If I run a server locally, which still uses the Azure SQL server, I don't have any issues.

This seems to be the same issue I have: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e20bb2a3-bdbe-4e49-8b92-b36fe50577da/having-trouble-deploying-flask-app-into-azure-with-azure-database?forum=windowsazurewebsitespreview&prof=required

but they say whether they actually got it working with a SQL database...

what do people think? Thanks

my requirements.txt

alembic==0.7.7
azure==0.11.1
Flask==0.10.1
Flask-Migrate==1.5.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.0
futures==3.0.3
itsdangerous==0.24
Jinja2==2.8
Mako==1.0.1
MarkupSafe==0.23
pymssql==2.1.1
python-dateutil==2.4.2
six==1.9.0
SQLAlchemy==1.0.8
Werkzeug==0.10.4
wheel==0.24.0

Per my understanding, Azure Web sites are hosted on Windows Server 2012 VM by default, which would not install FreeTDS and also we don't have permission to install it. And the pymssql is based on FreeTDS .

So we can use pyodbc to connect the Azure SQL as a workaround way to handle it in ease.

I was able to successfully connect my Python code to a SQL Database on Azure using the pymssql 2.1 library.

I don't know if this is what is causing the problem but I did include a couple of extra parameters in my connection string... maybe that will help. I specified the driver explicitly and the Encrypt parameter since SSL is always enabled for SQL Azure (I believe)

myConnection = pyodbc.connect('Driver={SQL Server};'
'Server=tcp:1234567.database.windows.net,1433;'
'Database=MyAzureDatabase;'
'Uid=geekgirl@123456;Pwd=abcdef;'
'Encrypt=yes')

If that doesn't work, maybe try adding some error handling around the connect statement to try and get a more specific error message.

Fingers crossed!

I had this problem, but I eventually realized it was because I was using the 64-bit version of Python in Visual Studio, but Azure only supports the 32-bit version.

I was seeing:

DLL load failed: %1 is not a valid Win32 application.

Once I switched to installing and using the 32-bit version I was able to use Flask and pyodbc with no problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM