【问题标题】:odbc connection error when transitioning django pyodbc application to apache将 django pyodbc 应用程序转换为 apache 时出现 odbc 连接错误
【发布时间】:2013-08-26 18:25:31
【问题描述】:

我尝试了以下修复: https://code.google.com/p/pyodbc/issues/detail?id=214

我仍然遇到同样的错误。

知道如何解决这种情况吗?

这里是 odbc dns 列表的截图

Python 代码

import pyodbc
import datetime as dt
from broker.models import Customer, Usage, Invoice, Document


def import_customers(user_id,m_code):
    cnxn = pyodbc.connect('DSN=as400;PWD=*********;DBQ=PRODDTA')
    cursor = cnxn.cursor()

    # the VNDRCODE of LQPPNATSLC is equivalent to the VENDORNUM of the LQPPNATVND !!!
    # the VNDRCODE of LQPPNATVND is ACTUALLY the decriptive name of the broker
    cursor.execute("""select b.CUSTNBR,
                             b.CONAME,
                             b.LASTNAME,
                             b.FIRSTNAME,
                             b.MIDDLEINT,
                             b.STREET1,
                             b.STREET2,
                             b.CITY,
                             b.STATE,
                             b.ZIPCODE,
                             b.ZIPEXTN,
                             b.PHONENBR,
                             a.PCID,
                             c.VNDRCODE as VNAME,
                             d.STREET1,
                             d.CITY,
                             d.STATE,
                             d.ZIPCODE,
                             a.VNDRCODE 
                    from LQPPNATSLC a, LQPPCUSMST b, LQPPNATVND c, LQPPDELDIR d
                    where a.CUSTNBR = b.CUSTNBR and
                          a.CUSTNBR = d.CUSTNBR and
                          a.VNDRCODE = c.VENDORNUM and
                          b.CUSTSTAT in ( '','RE') and
                          a.VNDRCODE = ? 
                    order by b.CUSTNBR """,str(m_code))

    Customer.objects.filter(user_id=user_id).delete()
    row = cursor.fetchone()
    while row:      
        b = Customer( user_id = user_id,
                    customer_number = row.CUSTNBR,
                    company_name = row.CONAME,
                    last_name = row.LASTNAME,
                    first_name = row.FIRSTNAME,
                    middle_initial = row.MIDDLEINT,
                    street_address1 = row.STREET1,
                    street_address2 = row.STREET2,
                    city = row.CITY,
                    state = row.STATE,
                    zip = row.ZIPCODE,
                    zip_ext = row.ZIPEXTN,
                    phone = row.PHONENBR,                   
                    PCID = row.PCID,
                    broker_name = row.VNAME, # see note above
                    service_address1 = row.STREET1,
                    service_city = row.CITY,
                    service_state = row.STATE,
                    service_zip = row.ZIPCODE,
                    broker_code = row.VNDRCODE.strip()
                    )
        b.save()
        row = cursor.fetchone()

来自 apache errror.log 的错误

[Mon Aug 26 13:50:41 2013] [error]   File "C:\\Python27\\Lib\\site-packages\\django\\core\\handlers\\base.py", line 115, in get_response
[Mon Aug 26 13:50:41 2013] [error]     response = callback(request, *callback_args, **callback_kwargs)
[Mon Aug 26 13:50:41 2013] [error]   File "C:\\Python27\\Lib\\site-packages\\django\\contrib\\auth\\decorators.py", line 25, in _wrapped_view
[Mon Aug 26 13:50:41 2013] [error]     return view_func(request, *args, **kwargs)
[Mon Aug 26 13:50:41 2013] [error]   File "C:\\dev\\broker_portal\\broker\\views.py", line 57, in customer_list
[Mon Aug 26 13:50:41 2013] [error]     as400.import_customers(user_id,marketer_code)
[Mon Aug 26 13:50:41 2013] [error]   File "C:\\dev\\broker_portal\\broker\\as400.py", line 7, in import_customers
[Mon Aug 26 13:50:41 2013] [error]     cnxn = pyodbc.connect('DSN=as400;PWD=*********;DBQ=PRODDTA')
[Mon Aug 26 13:50:41 2013] [error] Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Django 调试错误

Error at /broker/5/3/
('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Request Method: GET
Request URL:    http://djangodev.shipley.com/broker/5/3/
Django Version: 1.5.1
Exception Type: Error
Exception Value:    
('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Exception Location: C:\dev\broker_portal\broker\as400.py in import_customers, line 7
Python Executable:  C:\Apache2\bin\httpd.exe
Python Version: 2.7.5
Python Path:    
['C:\\dev\\broker_portal',
 'C:\\Python27',
 'C:\\Python27\\Lib\\site-packages',
 'C:\\Python27\\Scripts',
 'C:\\Apache2',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\Lib',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\Lib\\lib-tk',
 'C:\\Apache2\\bin']
Server time:    Mon, 26 Aug 2013 13:50:41 -0400

元数据

wsgi.multiprocess   
False
HTTP_COOKIE 
'csrftoken=ukCmTMgFUDtnSBvbOWYexafiTa4FPmmV; sessionid=qnh4u7n6mishbc4ka1oofxfd2fhtiraw'
SERVER_PROTOCOL 
'HTTP/1.1'
SERVER_SOFTWARE 
'Apache/2.2.25 (Win64) mod_wsgi/3.5-BRANCH Python/2.7.5'
SCRIPT_NAME 
u''
mod_wsgi.enable_sendfile    
'0'
mod_wsgi.handler_script 
''
SERVER_SIGNATURE    
''
REQUEST_METHOD  
'GET'
PATH_INFO   
u'/broker/5/3/'
SystemRoot  
'C:\\Windows'
QUERY_STRING    
''
HTTP_USER_AGENT 
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36'
HTTP_CONNECTION 
'keep-alive'
HTTP_REFERER    
'http://djangodev.shipley.com/accounts/login/'
SERVER_NAME 
'djangodev.shipley.com'
REMOTE_ADDR 
'10.1.6.182'
mod_wsgi.request_handler    
'wsgi-script'
wsgi.url_scheme 
'http'
PATH_TRANSLATED 
'C:\\dev\\broker_portal\\broker_portal\\wsgi.py\\broker\\5\\3\\'
SERVER_PORT 
'80'
mod_wsgi.version    
(3, 5)
mod_wsgi.input_chunked  
'0'
SERVER_ADDR 
'10.1.4.161'
DOCUMENT_ROOT   
'C:/Apache2/htdocs'
mod_wsgi.process_group  
''
COMSPEC 
'C:\\Windows\\system32\\cmd.exe'
SCRIPT_FILENAME 
'C:/dev/broker_portal/broker_portal/wsgi.py'
SERVER_ADMIN    
'admin@example.com'
wsgi.input  
<mod_wsgi.Input object at 0x00000000044BD9F0>
HTTP_HOST   
'djangodev.shipley.com'
mod_wsgi.callable_object    
'application'
wsgi.multithread    
True
PATHEXT 
'.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'
HTTP_CACHE_CONTROL  
'max-age=0'
REQUEST_URI 
'/broker/5/3/'
HTTP_ACCEPT 
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
WINDIR  
'C:\\Windows'
wsgi.version    
(1, 0)
GATEWAY_INTERFACE   
'CGI/1.1'
wsgi.run_once   
False
wsgi.errors 
<mod_wsgi.Log object at 0x00000000044924F0>
REMOTE_PORT 
'50753'
HTTP_ACCEPT_LANGUAGE    
'en-US,en;q=0.8'
mod_wsgi.application_group  
'DjangoDev.shipley.com|'
mod_wsgi.script_reloading   
'1'
wsgi.file_wrapper   
''
CSRF_COOKIE 
u'ukCmTMgFUDtnSBvbOWYexafiTa4FPmmV'
HTTP_ACCEPT_ENCODING    
'gzip,deflate,sdch'

httpd.conf 修改

AliasMatch ^/([^/]*\.css) /dev/broker_portal/broker/css/$1

# Alias /media/ /path/to/mysite.com/media/
Alias /static/ /dev/broker_portal/broker/static/

<Directory  /dev/broker_portal/broker/static>
Order deny,allow
Allow from all
</Directory>

#<Directory /path/to/mysite.com/media>
#Order deny,allow
#Allow from all
#</Directory>

WSGIScriptAlias / /dev/broker_portal/broker_portal/wsgi.py
WSGIPythonPath /dev/broker_portal

<Directory /dev/broker_portal/broker_portal>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so

LoadModule wsgi_module modules/mod_wsgi.so

【问题讨论】:

  • 我不明白链接的问题与您的问题有何关系。错误消息指出未找到名为 as400 的 DSN。是否定义了 DSN?如果是这样,您能否用类型的定义和建议更新您的问题(例如系统、用户、文件)?
  • 在同一台机器上使用 django runserver 定义并使用了过去三周。我正在将应用程序转移到 Apache 服务器,以准备将其投入生产。我将很快发布 DSN 和 odbc 设置的文档。
  • 尝试将 DSN 创建为系统 DSN。 Apache 用户应该以“系统”用户身份运行,而不是您。
  • 我在创建这些时以系统管理员身份登录。我将如何以不同的方式使其成为系统用户?我在对话框中没有看到任何与此相关的选项...
  • ok.Researched 它并创建了两个 dsn 的系统版本,现在一切正常! Benny Hill,如果你想写出来,我会把你的标记为正确答案。谢谢。

标签: python django apache pyodbc


【解决方案1】:

另一种方法是嵌入连接字符串,而不是在代码之外维护 DSN。您需要使用连接字符串语法,但这里有一些东西可以开始(我正在使用pyodbc.connect 文档中定义的混合字符串和关键字):

cnxn = pyodbc.connect('system=<insert system name';dbq='PRODDTA',
                      driver='{iSeries Access ODBC Driver}',
                      uid='<insert user name>', pwd='<insert password>')

我发现这种方法使不可避免的代码迁移更容易。

【讨论】:

    【解决方案2】:

    您需要将 DSN 设置为系统 DSN,因为 Apache 用户是“系统”用户,并且将查看系统 DSN 而不是用户 DSN。

    【讨论】:

      【解决方案3】:

      pyodbc 的这个连接字符串,为我工作:

      conexion_str = 'SYSTEM=%s;db2:DSN=%s;UID=%s;PWD=%s;DRIVER=%s;' % (self._SYSTEM, self._DSN, self._UID, self._PWD, self._DRIVER)
      cnn = pyodbc.connect(conexion_str)
      

      【讨论】:

        猜你喜欢
        • 2015-05-21
        • 2020-12-22
        • 2021-06-28
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多