栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python的集成Windows身份验证(NTLM)通过Exchange进行SMTP

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python的集成Windows身份验证(NTLM)通过Exchange进行SMTP

尽管下面的解决方案仅使用Python Win32扩展(Python Win32扩展附带的sspi示例代码非常有帮助),但问题中提到的python-ntlm
IMAP和SMTP修补程序也可作为有用的指南。

from smtplib import SMTPException, SMTPAuthenticationErrorimport stringimport base64import sspi# NTLM Guide -- http://curl.haxx.se/rfc/ntlm.htmlSMTP_EHLO_OKAY = 250SMTP_AUTH_CHALLENGE = 334SMTP_AUTH_OKAY = 235def asbase64(msg):    # encoding the message then convert to string    return base64.b64enpre(msg).depre("utf-8")def connect_to_exchange_as_current_user(smtp):    """Example:    >>> import smtplib    >>> smtp = smtplib.SMTP("my.smtp.server")    >>> connect_to_exchange_as_current_user(smtp)    """    # Send the SMTP EHLO command    pre, response = smtp.ehlo()    if pre != SMTP_EHLO_OKAY:        raise SMTPException("Server did not respond as expected to EHLO command")    sspiclient = sspi.ClientAuth('NTLM')    # Generate the NTLM Type 1 message    sec_buffer=None    err, sec_buffer = sspiclient.authorize(sec_buffer)    ntlm_message = asbase64(sec_buffer[0].Buffer)    # Send the NTLM Type 1 message -- Authentication Request    pre, response = smtp.docmd("AUTH", "NTLM " + ntlm_message)    # Verify the NTLM Type 2 response -- Challenge Message    if pre != SMTP_AUTH_CHALLENGE:        raise SMTPException("Server did not respond as expected to NTLM negotiate message")    # Generate the NTLM Type 3 message    err, sec_buffer = sspiclient.authorize(base64.deprebytes(response))    ntlm_message = asbase64(sec_buffer[0].Buffer)    # Send the NTLM Type 3 message -- Response Message    pre, response = smtp.docmd(ntlm_message)    if pre != SMTP_AUTH_OKAY:        raise SMTPAuthenticationError(pre, response)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517109.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号