<legend id='McC7y'><style id='McC7y'><dir id='McC7y'><q id='McC7y'></q></dir></style></legend>

<small id='McC7y'></small><noframes id='McC7y'>

    <bdo id='McC7y'></bdo><ul id='McC7y'></ul>
  • <tfoot id='McC7y'></tfoot>
    <i id='McC7y'><tr id='McC7y'><dt id='McC7y'><q id='McC7y'><span id='McC7y'><b id='McC7y'><form id='McC7y'><ins id='McC7y'></ins><ul id='McC7y'></ul><sub id='McC7y'></sub></form><legend id='McC7y'></legend><bdo id='McC7y'><pre id='McC7y'><center id='McC7y'></center></pre></bdo></b><th id='McC7y'></th></span></q></dt></tr></i><div id='McC7y'><tfoot id='McC7y'></tfoot><dl id='McC7y'><fieldset id='McC7y'></fieldset></dl></div>

      1. SMTP 通过 Exchange 使用 Python 集成 Windows 身份验证 (NTLM)

        时间:2023-07-02

        <small id='o9aK1'></small><noframes id='o9aK1'>

          <bdo id='o9aK1'></bdo><ul id='o9aK1'></ul>
          <legend id='o9aK1'><style id='o9aK1'><dir id='o9aK1'><q id='o9aK1'></q></dir></style></legend>

            <i id='o9aK1'><tr id='o9aK1'><dt id='o9aK1'><q id='o9aK1'><span id='o9aK1'><b id='o9aK1'><form id='o9aK1'><ins id='o9aK1'></ins><ul id='o9aK1'></ul><sub id='o9aK1'></sub></form><legend id='o9aK1'></legend><bdo id='o9aK1'><pre id='o9aK1'><center id='o9aK1'></center></pre></bdo></b><th id='o9aK1'></th></span></q></dt></tr></i><div id='o9aK1'><tfoot id='o9aK1'></tfoot><dl id='o9aK1'><fieldset id='o9aK1'></fieldset></dl></div>
            <tfoot id='o9aK1'></tfoot>
                    <tbody id='o9aK1'></tbody>
                  本文介绍了SMTP 通过 Exchange 使用 Python 集成 Windows 身份验证 (NTLM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用登录的 Windows 用户的凭据来验证与使用 NTLM 的 Exchange 服务器的 SMTP 连接.

                  I want to use the credentials of the logged-in Windows user to authenticate an SMTP connection to an Exchange server using NTLM.

                  我知道 python-ntlm 模块和 两个 补丁 为 SMTP 启用 NTLM 身份验证,但是我想使用当前用户的安全令牌并且没有提供用户名和密码.

                  I'm aware of the python-ntlm module and the two patches that enable NTLM authentication for SMTP, however I want to use the current user's security token and not have to supply a username and password.

                  与Windows Authentication with Python and urllib2的问题非常相似.p>

                  Very similar problem to Windows Authentication with Python and urllib2.

                  推荐答案

                  虽然下面的解决方案只使用了 Python Win32 扩展(Python Win32 扩展中包含的 sspi 示例代码非常有用),python-ntlm IMAP &问题中提到的 SMTP 补丁也可以作为有用的指南.

                  Although the solution below only uses the Python Win32 extensions (the sspi example code included with the Python Win32 extensions was very helpful), the python-ntlm IMAP & SMTP patches mentioned in the question also served as useful guides.

                  from smtplib import SMTPException, SMTPAuthenticationError
                  import string
                  import base64
                  import sspi
                  
                  # NTLM Guide -- http://curl.haxx.se/rfc/ntlm.html
                  
                  SMTP_EHLO_OKAY = 250
                  SMTP_AUTH_CHALLENGE = 334
                  SMTP_AUTH_OKAY = 235
                  
                  def asbase64(msg):
                      # encoding the message then convert to string
                      return base64.b64encode(msg).decode("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
                      code, response = smtp.ehlo()
                      if code != 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
                      code, response = smtp.docmd("AUTH", "NTLM " + ntlm_message)
                  
                      # Verify the NTLM Type 2 response -- Challenge Message
                      if code != 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.decodebytes(response))
                      ntlm_message = asbase64(sec_buffer[0].Buffer)
                  
                      # Send the NTLM Type 3 message -- Response Message
                      code, response = smtp.docmd(ntlm_message)
                      if code != SMTP_AUTH_OKAY:
                          raise SMTPAuthenticationError(code, response)
                  

                  这篇关于SMTP 通过 Exchange 使用 Python 集成 Windows 身份验证 (NTLM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Hotmail SSL3 版本号错误使用 smtp 下一篇:是否有 Python MTA(邮件传输代理)

                  相关文章

                  <small id='SyHSM'></small><noframes id='SyHSM'>

                  1. <i id='SyHSM'><tr id='SyHSM'><dt id='SyHSM'><q id='SyHSM'><span id='SyHSM'><b id='SyHSM'><form id='SyHSM'><ins id='SyHSM'></ins><ul id='SyHSM'></ul><sub id='SyHSM'></sub></form><legend id='SyHSM'></legend><bdo id='SyHSM'><pre id='SyHSM'><center id='SyHSM'></center></pre></bdo></b><th id='SyHSM'></th></span></q></dt></tr></i><div id='SyHSM'><tfoot id='SyHSM'></tfoot><dl id='SyHSM'><fieldset id='SyHSM'></fieldset></dl></div>
                    <tfoot id='SyHSM'></tfoot>

                    <legend id='SyHSM'><style id='SyHSM'><dir id='SyHSM'><q id='SyHSM'></q></dir></style></legend>
                      • <bdo id='SyHSM'></bdo><ul id='SyHSM'></ul>