【问题标题】:Python ldap macOS - ValueError: option errorPython ldap macOS - ValueError:选项错误
【发布时间】:2017-04-10 16:25:30
【问题描述】:

我正在尝试在 macOS Sierra 上设置 python-ldap。 当我尝试使用该模块时(它适用于我在 CentOS 上运行的实时环境) 我收到以下错误,在搜索时似乎与在 macOS 上安装 OpenLDAP 或 python-ldap 有关,但我还没有找到解释如何修复它的文章。

到目前为止,我已经通过自制软件安装了 OpenLDAP,但尚未解决问题:

错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/views.py", line 47, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/views.py", line 81, in login
    if form.is_valid():
  File "/usr/local/lib/python2.7/site-packages/django/forms/forms.py", line 169, in is_valid
    return self.is_bound and not self.errors
  File "/usr/local/lib/python2.7/site-packages/django/forms/forms.py", line 161, in errors
    self.full_clean()
  File "/usr/local/lib/python2.7/site-packages/django/forms/forms.py", line 371, in full_clean
    self._clean_form()
  File "/usr/local/lib/python2.7/site-packages/django/forms/forms.py", line 398, in _clean_form
    cleaned_data = self.clean()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 191, in clean
    self.user_cache = authenticate(username=username, password=password)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
    user = backend.authenticate(**credentials)
  File "/itapp/itapp/backend.py", line 39, in authenticate
    ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,settings.AD_CERT_FILE)
  File "/usr/local/lib/python2.7/site-packages/ldap/functions.py", line 135, in set_option
    return _ldap_function_call(None,_ldap.set_option,option,invalue)
  File "/usr/local/lib/python2.7/site-packages/ldap/functions.py", line 66, in _ldap_function_call
    result = func(*args,**kwargs)
ValueError: option error 

我已经按照下面的方法通过 brew 安装了 openldap

alexs-mbp:~ alex$ brew install openldap
Warning: openldap is a keg-only and another version is linked to opt.
Use `brew install --force` if you want to install this version
alexs-mbp:~ alex$ brew install openldap --force
Warning: openldap-2.4.44 already installed, it's just not linked.

我已经用 pip 安装了 python-ldap

alexs-mbp:~ alex$ sudo pip install python-ldap
The directory '/Users/alex/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/alex/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: python-ldap in /usr/local/lib/python2.7/site-packages
Requirement already satisfied: setuptools in /usr/local/lib/python2.7/site-packages (from python-ldap

【问题讨论】:

标签: python django macos


【解决方案1】:

我的解决方案是在执行pip install python-ldap 时需要包含基于 brew 的 openldap 库

您需要的信息可以通过brew info openldap 找到,其中显示如下:

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/brew/opt/openldap/lib
    CPPFLAGS: -I/usr/local/brew/opt/openldap/include

我的 openldap 安装在 /usr/local/brew/opt/openldap/所以这需要一个类似的命令:

LDFLAGS="-L/usr/local/brew/opt/openldap/lib" CPPFLAGS="-I/usr/local/brew/opt/openldap/include" pip install python-ldap

尝试卸载 python-ldap (pip uninstall python-ldap) 然后运行brew info openldap 并使用您的特定路径打开ldap lib并在上述命令中包含目录

注意:我的 clang 编译器也有一个问题,找不到 MacOS SDK sasl.h,但我不知道这是否只是我的问题。通过在上面的命令行中添加--global-option=build_ext --global-option="-I$(xcrun --show-sdk-path)/usr/include/sasl" 解决了这个问题

【讨论】:

  • 应该说,确保你先做一个pip uninstall python-ldap,这样当你做install时它会重新编译——我也要编辑答案
  • 它也可以在 Catalina 上运行,只需要更新 LDFLAGS 和 CPPFLAGS,对我来说这个命令就可以了 LDFLAGS="-L/usr/local/opt/openldap/lib" CPPFLAGS="-I/usr/local/opt/openldap/include" pip3 install python-ldap --global-option=build_ext --global-option="-I$(xcrun --show-sdk-path)/usr/include/sasl"
  • 这很有效,谢谢
【解决方案2】:

我在 macOS Sierra 10.12.4 上使用 MacPorts 安装了 python-ldap(版本 2.3.10_3):

sudo port install py27-ldap

这还拉取了 OpenLDAP 依赖项(版本 2.4.44_2)。

我可以执行代码:

import ldap
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE,'cacert-2017-01-18.pem')

没有错误(来自https://curl.haxx.se/ca/cacert-2017-01-18.pem 的 CA 证书文件)

您按照哪些步骤安装 OpenLDAP 和 python-ldap?

编辑:

我想我找到了something relevant,你能用我链接的证书文件试试上面的测试代码吗?

我怀疑权限/.pem 格式错误

【讨论】:

  • 我已将我的安装方法添加到问题中,谢谢
  • 我下载了那个文件,然后 cd 到该目录并从该目录运行 python,然后上面的代码,得到相同的 ValueError:
猜你喜欢
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2015-12-03
  • 2015-12-05
相关资源
最近更新 更多