Exchange 邮件过滤器问题 Python
Exchange mail filter problems Python
我有一个脚本来列出来自交换帐户的电子邮件,但它需要很长时间才能收到回复,有时我会收到类似这样的错误。
EXCEPTION IN (<string>, L_203 ""): HTTPSConnectionPool(host='outlook.office365.com', port=443): Read timed out.
或
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'Se ha forzado la interrupción de una conexión existente por el host remoto', None, 10054, None)", ConnectionResetError(10054, 'Se ha forzado la interrupción de una conexión existente por el host remoto', None, 10054, None))
我不明白为什么要花这么长时间。我的帐户会不会有问题?有时最多需要 2 分钟才会出现错误
这是我的代码的一部分:
class ExchangeModule:
def __init__(self, user_, pwd, server_, mail_):
self.pwd = pwd
self.mail = mail_
self.server = server_
self.user = user_
self.credentials = None
self.config = None
def init(self):
from exchangelib import Credentials, Configuration
self.credentials = Credentials(username=self.user, password=self.pwd)
self.config = Configuration(server=self.server, credentials=self.credentials)
return self.config
if module == "exchange":
user = GetParams('user')
password = GetParams('pass')
server = GetParams('server')
address = GetParams('address')
print('USUARIO', user)
exchange_module = ExchangeModule(user, password, server, address)
config = exchange_module.init()
if module == "get_new_mail":
if exchange_module.config is not None:
config = exchange_module.config
else:
raise Exception("Execute Email Configuration command")
address = exchange_module.mail
a = Account(primary_smtp_address=address, config=config,
access_type=DELEGATE, autodiscover=False)
var = GetParams('var')
filter_type= GetParams('tipo_filtro')
filtro = GetParams('filtro')
id_ = []
if filtro:
if filter_type== 'author':
for m in a.inbox.all():
if not m.is_read and filtro in m.author.email_address:
id_.append(m.id)
if tipo_filtro == 'subject':
mails_filters = a.inbox.filter(subject__contains=filtro)
for m in mails_filters:
if not m.is_read:
id_.append(m.id)
# print('FOR',m.id)
else:
id_ = [m.id for m in a.inbox.all() if not m.is_read]
SetVar(var, id_)
如果您在 exchangelib 中启用调试日志记录(请参阅 https://ecederstrand.github.io/exchangelib/#troubleshooting),您可以确切地看到发生了什么。
您的脚本正在获取 所有 项字段,尽管它实际上只需要 id
字段。这非常繁重,因为您还要获取完整的正文、附件等。此外,它在客户端过滤项目,而这些项目也可能在服务器端被过滤,让您传输的数据少得多。而不是:
for m in a.inbox.all():
if not m.is_read and filtro in m.author.email_address:
id_.append(m.id)
做
for m in a.inbox.filter(is_read=True, author__contains=filtro).only('id'):
id_.append(m.id)
而不是:
mails_filters = a.inbox.filter(subject__contains=filtro)
for m in mails_filters:
if not m.is_read:
id_.append(m.id)
做
for m in a.inbox.filter(subject__contains=filtro, is_read=True).only('id'):
id_.append(m.id)
除此之外,您的 Internet 连接速度可能很慢。如果您想隐藏这些错误并让您的脚本重试,您可以配置容错 (https://ecederstrand.github.io/exchangelib/#fault-tolerance)。
我有一个脚本来列出来自交换帐户的电子邮件,但它需要很长时间才能收到回复,有时我会收到类似这样的错误。
EXCEPTION IN (<string>, L_203 ""): HTTPSConnectionPool(host='outlook.office365.com', port=443): Read timed out.
或
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'Se ha forzado la interrupción de una conexión existente por el host remoto', None, 10054, None)", ConnectionResetError(10054, 'Se ha forzado la interrupción de una conexión existente por el host remoto', None, 10054, None))
我不明白为什么要花这么长时间。我的帐户会不会有问题?有时最多需要 2 分钟才会出现错误
这是我的代码的一部分:
class ExchangeModule:
def __init__(self, user_, pwd, server_, mail_):
self.pwd = pwd
self.mail = mail_
self.server = server_
self.user = user_
self.credentials = None
self.config = None
def init(self):
from exchangelib import Credentials, Configuration
self.credentials = Credentials(username=self.user, password=self.pwd)
self.config = Configuration(server=self.server, credentials=self.credentials)
return self.config
if module == "exchange":
user = GetParams('user')
password = GetParams('pass')
server = GetParams('server')
address = GetParams('address')
print('USUARIO', user)
exchange_module = ExchangeModule(user, password, server, address)
config = exchange_module.init()
if module == "get_new_mail":
if exchange_module.config is not None:
config = exchange_module.config
else:
raise Exception("Execute Email Configuration command")
address = exchange_module.mail
a = Account(primary_smtp_address=address, config=config,
access_type=DELEGATE, autodiscover=False)
var = GetParams('var')
filter_type= GetParams('tipo_filtro')
filtro = GetParams('filtro')
id_ = []
if filtro:
if filter_type== 'author':
for m in a.inbox.all():
if not m.is_read and filtro in m.author.email_address:
id_.append(m.id)
if tipo_filtro == 'subject':
mails_filters = a.inbox.filter(subject__contains=filtro)
for m in mails_filters:
if not m.is_read:
id_.append(m.id)
# print('FOR',m.id)
else:
id_ = [m.id for m in a.inbox.all() if not m.is_read]
SetVar(var, id_)
如果您在 exchangelib 中启用调试日志记录(请参阅 https://ecederstrand.github.io/exchangelib/#troubleshooting),您可以确切地看到发生了什么。
您的脚本正在获取 所有 项字段,尽管它实际上只需要 id
字段。这非常繁重,因为您还要获取完整的正文、附件等。此外,它在客户端过滤项目,而这些项目也可能在服务器端被过滤,让您传输的数据少得多。而不是:
for m in a.inbox.all():
if not m.is_read and filtro in m.author.email_address:
id_.append(m.id)
做
for m in a.inbox.filter(is_read=True, author__contains=filtro).only('id'):
id_.append(m.id)
而不是:
mails_filters = a.inbox.filter(subject__contains=filtro)
for m in mails_filters:
if not m.is_read:
id_.append(m.id)
做
for m in a.inbox.filter(subject__contains=filtro, is_read=True).only('id'):
id_.append(m.id)
除此之外,您的 Internet 连接速度可能很慢。如果您想隐藏这些错误并让您的脚本重试,您可以配置容错 (https://ecederstrand.github.io/exchangelib/#fault-tolerance)。