在azure上托管的Flask web应用在POST和DB活动期间出现500个错误

2024-10-05 10:03:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经将一个使用visualstudio(azuredevops)构建的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 据我所知,错误主要与数据库连接有关。请在下面找到应用程序副本, 初始py以及web.config文件文件

在应用程序副本在

^{pr2}$

在初始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)

Tags: inpyselfappflaskhomesqlalchemylib

热门问题