Python ldap3 thumbnailphoto from Active Directory



python新手。因此,我使用Python LDAP3从Acitve目录中获得了一些属性,并保存在MySQL数据库中 - 一切都很好。我还从广告中获取thumbnailphoto属性,并在同一MySQL db表上保存为斑点。

这是问题,我需要在Base64类型的文本中使用它,但似乎以其他一些文本格式返回。ldap/mysql

的示例类型文本

我相信它应该保存在Active Directory上的Base64(基本64结果(中,但事实并非如此。

如何将此文本从AD通过LDAP3转换为Base64并保存到MySQL数据库?

from ldap3 import Server, Connection, ALL
from datetime import datetime
import MySQLdb
startTime = datetime.now()
# ######################
# LDAP Setup
# ######################
serverName = 'ip'
domainName = 'xyz'
userName = 'xyz'
password = 'xyz'
base = 'base'
# ######################
# MySQL Connection Setup
# ######################
connsql = MySQLdb.connect(host = "ip",
                               user = "xyz",
                               passwd = "xyz",
                               db = "db",
                               port=3306
                               )
# ######################
# Main Code
# ######################
server = Server(serverName)
conn = Connection(server, read_only=True, user='{0}\{1}'.format(domainName, userName), password=password, auto_bind=True)
conn.search(base, '(objectclass=person)', attributes=['sAMAccountName', 'displayName', 'mail', 'telephoneNumber', 'mobile', 'ipPhone', 'physicalDeliveryOfficeName', 'thumbnailPhoto'])
cursor = connsql.cursor ()
for i in conn.entries:
    cursor.execute("""
            INSERT INTO fwad
                (sAMAccountName, displayName, mail, telephoneNumber, mobile, ipPhone, physicalDeliveryOfficeName, thumbnailPhoto)
            VALUES
                (%s, %s, %s, %s, %s, %s, %s, %s)
            ON DUPLICATE KEY UPDATE
                displayName = VALUES(displayName),
                mail = VALUES(mail),
                telephoneNumber = VALUES(telephoneNumber),
                mobile = VALUES(mobile),
                ipPhone = VALUES(ipPhone),
                physicalDeliveryOfficeName = VALUES(physicalDeliveryOfficeName),
                thumbnailPhoto = VALUES(thumbnailPhoto);
                """,(i.sAMAccountName, i.displayName, i.mail, i.telephoneNumber, i.mobile, i.ipPhone, i.physicalDeliveryOfficeName, i.thumbnailPhoto))   
connsql.commit()
cursor .close()
connsql.close()
print ("AD has taken ", datetime.now() - startTime, " on ", datetime.now())

如果我了解您的问题,您可能可以解决您的问题:

import base64
...
...
imgstring = conn.response[0].get('attributes', {}).get('thumbnailPhoto', '')
img_string_decoded = base64.encodebytes(imgstring).decode('utf-8')

最新更新