我正在尝试获取LDAP数据库中存储的所有电子邮件地址的列表,用逗号分隔。你知道吗
通过简化这个script,我有:
#!/usr/bin/env python
# encoding: utf-8
# Author: Zhang Huangbin <zhb _at_ iredmail.org>
# Purpose: Add enabledService=lib-storage for all mail users.
# Required by IMAP folder sharing in Dovecot-2.0.
# Date: 2012-05-18
import sys
import ldap
# Note:
# * bind_dn must have write privilege on LDAP server.
uri = 'ldap://127.0.0.1:389'
basedn = 'o=domains,dc=example,dc=com'
bind_dn = 'cn=Manager,dc=example,dc=com'
bind_pw = 'password'
# Initialize LDAP connection.
print >> sys.stderr, "* Connecting to LDAP server: %s" % uri
conn = ldap.initialize(uri=uri, trace_level=0,)
conn.bind_s(bind_dn, bind_pw)
# Get all mail users.
print >> sys.stderr, "* Get all mail accounts..."
allUsers = conn.search_s(
basedn,
ldap.SCOPE_SUBTREE,
"(objectClass=mailUser)",
['mail', 'enabledService'],
)
total = len(allUsers)
print >> sys.stderr, "* Total %d user(s)." % (total)
# Counter.
count = 1
for user in allUsers:
(dn, entry) = user
mail = entry['mail'][0]
print >> "%s, " % (mail)
count += 1
# Unbind connection.
conn.unbind()
运行此命令时,出现错误:
- Connecting to LDAP server: ldap://127.0.0.1:389
- Get all mail accounts...
- Total 64 user(s). Traceback (most recent call last): File "list_mail_users.py", line 43, in print >> "%s, " % (mail) AttributeError: 'str' object has no attribute 'write'
我在支持论坛上问了这个问题,他们建议我改用:ldapsearch?你知道吗
这是你的问题
您正试图打印到“%s”,它是一个字符串,print only可以接受具有write属性的对象。我不太确定你想用它做什么,但我希望是
print >> sys.stdout, "%s, " %(mail)
或者print >> File "%s, "%mail
相关问题 更多 >
编程相关推荐