当 运行 来自 apache2/mod_wsgi 时,Turbodbc returns ODBC 错误状态 60

Turbodbc returns ODBC error state 60 when run from apache2/mod_wsgi

我有一个 Django 网站,它使用 TURBODBC 对遗留数据库执行一些直接 SQL 调用。这在使用内置的 django 测试服务器时非常有效,但是当我 运行 它与 Apache2 一起使用时,我收到错误消息:“ODBC 错误状态:60”。我觉得它与 运行 和 mod_wsgi 有关系,但我不确定。我在 Google 上没有运气。甚至不确定“state: 60”是什么意思。

Internal Server Error: /inventory/in/
[Mon Apr 09 22:01:09.718077 2018] [wsgi:error] [pid 10074:tid 140709839501056]  Traceback (most recent call last):
[Mon Apr 09 22:01:09.718147 2018] [wsgi:error] [pid 10074:tid 140709839501056]    File "/usr/local/lib/python3.5/dist-packages/turbodbc/exceptions.py", line 50, in wrapper
[Mon Apr 09 22:01:09.718213 2018] [wsgi:error] [pid 10074:tid 140709839501056]     return f(*args, **kwds)
[Mon Apr 09 22:01:09.718282 2018] [wsgi:error] [pid 10074:tid 140709839501056]    File "/usr/local/lib/python3.5/dist-packages/turbodbc/connect.py", line 44, in connect
[Mon Apr 09 22:01:09.718347 2018] [wsgi:error] [pid 10074:tid 140709839501056]      turbodbc_options))
[Mon Apr 09 22:01:09.718410 2018] [wsgi:error] [pid 10074:tid 140709839501056]  turbodbc_intern.Error: ODBC error
[Mon Apr 09 22:01:09.718493 2018] [wsgi:error] [pid 10074:tid 140709839501056] ] state: 60 

问题: "state 60" 是什么意思? 是什么导致了这个错误? 我该如何解决这个问题?

重现错误的简单程序。

from turbodbc import connect


def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'
    connect_to_db()
    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]


def connect_to_db():
    connection = connect(dsn="odbc-dev", uid='foo', pwd='bar')
    cursor = connection.cursor()

apache2 配置:

WSGIScriptAlias / /srcPython3/wsgi_test/hello_world.py

WSGIPythonPath /srcPython3/wsgi_test/

<VirtualHost *:80>
    <Directory  /srcPython3/wsgi_test>
        <Files hello_world.py>
            Require all granted
        </Files>
    </Directory>
</VirtualHost>

error.log

mod_wsgi (pid=11944): Exception occurred processing WSGI script '/srcPython3/wsgi_test/hello_world.py'.
[wsgi:error]  Traceback (most recent call last):
[wsgi:error]  File "/usr/local/lib/python3.5/dist-packages/turbodbc/exceptions.py", line 50, in wrapper
[wsgi:error]     return f(*args, **kwds)
[wsgi:error]   File "/usr/local/lib/python3.5/dist-packages/turbodbc/connect.py", line 44, in connect
[wsgi:error]     turbodbc_options))
[wsgi:error] turbodbc_intern.Error: ODBC error
[wsgi:error] state: 60
[wsgi:error]
[wsgi:error] During handling of the above exception, another exception occurred:
[wsgi:error]
[wsgi:error] Traceback (most recent call last):
[wsgi:error]  File "/srcPython3/wsgi_test/hello_world.py", line 7, in application
[wsgi:error]     connect_to_db()
[wsgi:error]   File "/srcPython3/wsgi_test/hello_world.py", line 15, in connect_to_db
[wsgi:error]     connection = connect(dsn="odbc-dev", uid='foo', pwd='bar')
[wsgi:error]   File "/usr/local/lib/python3.5/dist-packages/turbodbc/exceptions.py", line 52, in wrapper
[wsgi:error]     raise DatabaseError(str(e))
[wsgi:error] turbodbc.exceptions.DatabaseError: ODBC error
[wsgi:error] state: 60

谢谢

在 mod_wsgi 下访问数据库可能出现的问题之一是您的代码 运行 是 Apache 用户而不是您。此外,环境变量不会从您的用户登录配置文件继承。

因此,如果访问机制导致使用 UNIX 帐户代码的用户名是 运行ning,则它将不起作用。如果您在您的个人帐户中设置环境变量并且您的代码期望看到这些变量,则将不起作用。

对于第一个问题,确保您使用的是 mod_wsgi 守护进程模式,然后将守护进程模式配置为 运行 您的代码,而不是 Apache 用户。

对于第二个,在导入任何其他代码之前在 WSGI 脚本文件中设置环境变量。