【问题标题】:Turbodbc returns ODBC error state 60 when run from apache2/mod_wsgiTurbodbc 从 apache2/mod_wsgi 运行时返回 ODBC 错误状态 60
【发布时间】:2018-04-10 00:42:09
【问题描述】:

我有一个 Django 网站,它使用 TURBODBC 在旧数据库上执行一些直接 SQL 调用。这在使用内置 django 测试服务器时非常有效,但是当我使用 Apache2 运行它时,我收到错误消息:“ODBC 错误状态:60”。我觉得它与使用 mod_wsgi 运行它有关,但我不确定。我对谷歌没有运气。甚至不确定“状态: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 

问题: “状态 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>

错误日志

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

谢谢

【问题讨论】:

标签: django python-3.x odbc apache2 mod-wsgi


【解决方案1】:

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

所以如果访问机制导致使用 UNIX 帐户代码的用户名运行,它将无法正常工作。如果您在您的个人帐户中设置环境变量并且您的代码希望看到这些,则将无法正常工作。

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

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

【讨论】:

  • 你的评价很到位。请查看我在turbodbc github page 上的对话。我通过在 /etc/apache2/envvars 末尾添加“export ODBCINI=/etc/odbc.ini”解决了这个问题
猜你喜欢
  • 2017-08-03
  • 2014-02-03
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 2012-05-25
  • 2015-10-13
相关资源
最近更新 更多