我已将使用Visual Studio(Azure Devops)构建的Flask Web应用程序迁移到Azure应用程序服务。 flask Web应用程序可与MSSQL DB一起使用,并对所有DB函数使用sqlalchemy。迁移后的应用程序可以完美地加载到浏览器中,接受与用户登录,注册或任何其他数据库POST,GET活动相关的任何功能。这些导致500错误,如下所示:
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.
500 ERROR Image 据我了解,该错误主要与数据库连接有关。请在以下app.py,init.py和web.config文件中找到
App.py
############ Main Run Engine ################################
from KoreD import app ##### Main Project Package ######
if __name__ == '__main__':
app.run(debug=True)
#Using PyOpenSSL as library and below code, HTTPS connection works well
# app.run(debug=True, threaded=True,ssl_context='adhoc')
init.py
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from datetime import timedelta
import flask_excel as excel
import urllib
# from flask_static_compress import FlaskStaticCompress
#from flask_user import UserManager
app = Flask(__name__)
# FlaskStaticCompress(app)
#############################################################################
############ CONFIGURATIONS ###############
###########################################################################
app.config['SECRET_KEY'] = 'XXXXXXXXXXXXXXXXXXX'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://PASSWORD:USERNAME@korederivatives.database.windows.net/KoreD?driver={SQL Server}'
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=60)
app.config['CSRF_ENABLED'] = True
db = SQLAlchemy(app)
Migrate(app,db)
web.config
<configuration>
<appSettings>
<add key="pythonpath" value="%SystemDrive%\home\site\wwwroot" />
<add key="WSGI_HANDLER" value="manage.app" />
<add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="*" verb="*" modules="FastCgiModule"
scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py"
resourceType="Unspecified" requireAccess="Script"/>
</handlers>
</system.webServer>
<connectionStrings>
<clear />
<add name="KoreD"
providerName="System.Data.SqlClient"
connectionString= "server=korederivatives.database.windows.net;database=DBNAME;uid=USERNMAE;pwd=PASSWORD" />
</connectionStrings>
</configuration>
我无法弄清这500个错误的原因,并希望得到社区的反馈。
干杯!
更新
我能够收集以下错误日志:
Traceback (most recent call last):
File "D:\home\python364x64\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "D:\home\python364x64\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\home\python364x64\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\home\python364x64\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "D:\home\python364x64\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "D:\home\python364x64\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\home\python364x64\lib\site-packages\flask_user\user_manager.py", line 416, in login_stub
return self.login_view()
File "D:\home\python364x64\lib\site-packages\flask_user\user_manager__views.py", line 378, in login_view
if request.method == 'POST' and login_form.validate():
File "D:\home\python364x64\lib\site-packages\flask_user\forms.py", line 199, in validate
user, user_email = user_manager.db_manager.get_user_and_user_email_by_email(self.email.data)
File "D:\home\python364x64\lib\site-packages\flask_user\db_manager.py", line 173, in get_user_and_user_email_by_email
user = self.db_adapter.ifind_first_object(self.UserClass, email=email)
File "D:\home\python364x64\lib\site-packages\flask_user\db_adapters\sql_db_adapter.py", line 139, in ifind_first_object
return query.first()
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\query.py", line 2895, in first
ret = list(self[0:1])
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\query.py", line 2687, in __getitem__
return list(res)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\query.py", line 2995, in __iter__
return self._execute_and_instances(context)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\query.py", line 3016, in _execute_and_instances
close_with_result=True)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\query.py", line 3025, in _get_bind_args
**kw
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\query.py", line 3007, in _connection_from_session
conn = self.session.connection(**kw)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\session.py", line 1046, in connection
execution_options=execution_options)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\session.py", line 1051, in _connection_for_bind
engine, execution_options)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\orm\session.py", line 409, in _connection_for_bind
conn = bind.contextual_connect()
File "D:\home\python364x64\lib\site-packages\sqlalchemy\engine\base.py", line 2123, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "D:\home\python364x64\lib\site-packages\sqlalchemy\engine\base.py", line 2162, in _wrap_pool_connect
e, dialect, self)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception_noconnection
exc_info
File "D:\home\python364x64\lib\site-packages\sqlalchemy\util\compat.py", line 265, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\util\compat.py", line 248, in reraise
raise value.with_traceback(tb)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\engine\base.py", line 2158, in _wrap_pool_connect
return fn()
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 400, in connect
return _ConnectionFairy._checkout(self)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 788, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 529, in checkout
rec = pool._do_get()
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 1193, in _do_get
self._dec_overflow()
File "D:\home\python364x64\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\util\compat.py", line 249, in reraise
raise value
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 1190, in _do_get
return self._create_connection()
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 347, in _create_connection
return _ConnectionRecord(self)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 474, in __init__
self.__connect(first_connect_check=True)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\pool.py", line 671, in __connect
connection = pool._invoke_creator(self)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\engine\strategies.py", line 106, in connect
return dialect.connect(*cargs, **cparams)
File "D:\home\python364x64\lib\site-packages\sqlalchemy\engine\default.py", line 412, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM012', '[IM012] [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/dbapi)