如何解决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 举报,一经查实,本站将立刻删除。