Exchangelib TLS配置

问题描述

我一直在尝试使用以下代码访问组织的交换服务器中的邮箱:

from exchangelib import Credentials,Account,DELEGATE,Configuration,NTLM,IMPERSONATION,FaultTolerance,EWSDateTime,EWSTimeZone,Message,Q
import datetime 
import getpass
import logging
logging.basicConfig(level=logging.DEBUG)

 
user_name = 'Test.Helpdesk'
new_password = getpass.getpass(prompt='Password: ',stream=None)

try:
    credentials = Credentials(username = user_name,password = new_password)
    config = Configuration(server ='example.com',credentials = credentials)
    account = Account(primary_smtp_address = "Test.Helpdesk@example.com",credentials = credentials,config = config,autodiscover = False)
    print("Success")

except Exception as e:
    print(e)

我每次都会遇到相同的错误:

HTTPSConnectionPool(host='example.com',port=443): Max retries exceeded with url: /EWS/Exchange.asmx (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000178A6AD8608>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

我尝试遵循以下SO帖子:

SSL: CERTIFICATE_VERIFY_FAILED when connecting to a company Exchange Server

此:

import requests
print (requests.certs.where())

请给我找到.pem文件的目录路径。

然后这个:

import ssl

context = ssl.create_default_context()
der_certs = context.get_ca_certs(binary_form=True)
pem_certs = [ssl.DER_cert_to_PEM_cert(der) for der in der_certs]

with open('wincacerts.pem','w') as outfile:
    for pem in pem_certs:
        outfile.write(pem + '\n')

我将名称“ wincacerts”替换为“ cacerts”,因为这是我在先前代码指向我的目录中找到的文件的名称。

然后这个:

import os
import requests

root_path = os.getcwd()
path_pem=os.path.join(".....\\cacerts.pem")
requests.get('https://example.com',verify=path_pem

给我:<Response [401]>我不确定这是什么意思。

现在,最后,根据SO帖子(上面给出的链接)的解决方案,我应该使用以下代码并根据需要进行更改:

#EXAMPLE来自另一个。开机自检

root_path = os.getcwd()
path_pem=os.path.join(root_path,'files','wincacerts.crt')

class RootCAAdapter(requests.adapters.HTTPAdapter):
    # An HTTP adapter that uses a custom root CA certificate at a hard coded location
    def cert_verify(self,conn,url,verify,cert):
        cert_file = {
            'mail.ourserver.loc': path_pem,'mail.internal': '/path/to/mail.internal.crt'
            }[urlparse(url).hostname]
        super(RootCAAdapter,self).cert_verify(conn=conn,url=url,verify=cert_file,cert=cert)

# Tell exchangelib to use this adapter class instead of the default
BaseProtocol.HTTP_ADAPTER_CLS = RootCAAdapter

问题是我不了解SSL和证书以及它们如何工作,因此我不知道上述代码中对我有用的更改是什么。 另外,我还获悉组织中的服务器只能在TLS 1.2上运行。

这些是我的网络部门提供的确切信息:

确保您的应用程序配置了TLS1.2。 服务器仅支持TLS 1.2。

有没有办法在exchangelib代码中将TLS配置为1.2?

更新

我遇到了此链接https://github.com/ecederstrand/exchangelib/issues/155 并提到可以使用HTTPAdapter class解决此问题。

如果有人能够阐明类的工作方式以及需要根据个人需求进行更改的变量,那将有很大的帮助。

https://ecederstrand.github.io/exchangelib/文档中没有太多讨论

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...