Active Directory中的Python ldap3缩略图照片

时间:2019-05-22 14:58:39

标签: python-3.x active-directory ldap

Python新手。因此,我正在使用python ldap3从acitve目录中获取一些属性,并将其保存在mysql数据库中-一切正常。 我还从广告获得了thumbnailphoto属性,并另存为同一mysql db表中的blob。

这是问题,我需要使用base64类型的文本,但是它似乎是以其他某种文本格式返回的。 sample type text from ldap / mysql

我相信应该将其保存在活动目录中的base64(base64 result required)中,但事实并非如此。

如何将通过ldap3从AD返回的文本转换为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())

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则可以通过以下方法解决您的问题:

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