1

我需要迭代文件夹中的所有电子邮件,最终导出为 CSV 格式。这是代码的重要部分:

credentials = ServiceAccount(user, password)
...
qs = account.inbox.all()
qs.page_size = 100
qs.order_by('-datetime_received')
emails = qs.iterator()
for i, email in enumerate(emails):
    print(i, email.subject, email.sender, email.datetime_received)

当我运行时,我每次都会收到错误,但不是在过程中的同一点。有时我会返回一页(i == 99),有时我会返回三页(i==299)或返回五页(i==499)。五页是我得到的最多的。不管错误发生在哪里,都是这个错误。FWIW,错误确实发生在分页符处。

    Traceback (most recent call last):
  File "outlook.py", line 81, in <module>
    for i, email in enumerate(emails):
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\queryset.py", line 390, in _as_items
    for i in iterable:
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\account.py", line 628, in fetch
    shape=IdOnly,
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\services.py", line 602, in _pool_requests
    for elem in r.get():
  File "C:\dev\programs\anaconda3\lib\multiprocessing\pool.py", line 644, in get
    raise self._value
  File "C:\dev\programs\anaconda3\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\services.py", line 591, in <lambda>
    lambda c: self._get_elements(payload=payload_func(c, **kwargs)),
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\services.py", line 90, in _get_elements
    response = self._get_response_xml(payload=payload)
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\services.py", line 174, in _get_response_xml
    res = self._get_soap_payload(soap_response=soap_response_payload)
  File "C:\dev\programs\anaconda3\lib\site-packages\exchangelib\services.py", line 229, in _get_soap_payload
    raise SOAPError('Unknown SOAP response: %s' % xml_to_str(body))
exchangelib.errors.SOAPError: Unknown SOAP response: <ns0:Body xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" />

这是浏览所有电子邮件的正确方法吗?这个错误是什么意思,我该如何解决?谢谢!

更新:以下是捕获的输出。我们看不到任何看起来像“退避”消息的东西。这是否有助于您识别任何问题?

<< This run had page size set to 10 >>
DEBUG:exchangelib.util:Session 5122 thread 9556: Useful response from https://our.host.name/EWS/Exchange.asmx
DEBUG:exchangelib.protocol:Server our.host.name: Releasing session 5122
DEBUG:exchangelib.services:Trying API version Exchange2010_SP2 for account myaccount@myhost.com
DEBUG:exchangelib.services:FindItem: Got page with next offset 360 (last_page False)
DEBUG:exchangelib.services:Starting GetItem._get_elements worker 36 for 10 items
DEBUG:exchangelib.services:GetItem._get_elements result 30 is ready early

<< 10 of these DEBUG lines come out >>
DEBUG:exchangelib.services:Getting item ('AAMkADJjMDIwNmQwLWQ2NzYtNDk5OC04MjgxLT
MzZTdhMTk4YWRiNQBGAAAAAABGQ9HU4LA/RY8Mwwr5Jp5OBwDOJO2TCOmBSZeM4xH+g3x5AAAAiE7BAA
DOJO2TCOmBSZeM4xH+g3x5AAAe2nAfAAA=', 'CQAAABYAAADOJO2TCOmBSZeM4xH+g3x5AAAe2wDA')

DEBUG:exchangelib.services:Getting item ('AAMkADJjMDIwNmQwLWQ2NzYtNDk5OC04MjgxLT
MzZTdhMTk4YWRiNQBGAAAAAABGQ9HU4LA/RY8Mwwr5Jp5OBwDOJO2TCOmBSZeM4xH+g3x5AAAAiE7BAA
DOJO2TCOmBSZeM4xH+g3x5AAAe2nAeAAA=', 'CQAAABYAAADOJO2TCOmBSZeM4xH+g3x5AAAe2wC+')  

...

<<Seemed to process the above page successfully, then starts w/ errors >>
File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\response.py", line 502, in read_chunked
DEBUG:exchangelib.protocol:Server our.host.name: Waiting for session
DEBUG:exchangelib.protocol:Server our.host.name: Got session 5122
DEBUG:exchangelib.util:Session 5122 thread 6408: retry 0 timeout 120000 
    POST'ing to https://our.host.name/EWS/Exchange.asmx after 1000s wait self._update_chunk_length()
File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\response.py", line 448, in _update_chunk_length
    line = self._fp.fp.readline()
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\socket.py", line 575, in readinto
    return self._sock.recv_into(b)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\ssl.py", line 929, in recv_into
    return self.read(nbytes, buffer)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\ssl.py", line 791, in read
    return self._sslobj.read(len, buffer)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\ssl.py", line 575, in read
    v = self._sslobj.read(len, buffer)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\models.py", line 676, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\response.py", line 353, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\response.py", line 530, in read_chunked
    self._original_response.close()
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\packages\urllib3\response.py", line 250, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
requests.packages.urllib3.exceptions.ProtocolError: ("Connection broken: 
    ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", 
    ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "email_test.py", line 27, in <module>
    for i, email in enumerate(emails):
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\queryset.py", line 390, in _as_items
    for i in iterable:
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\account.py", line 628, in fetch
    shape=IdOnly,
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\services.py", line 596, in _pool_requests
    for elem in r.get():
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\multiprocessing\pool.py", line 608, in get
    raise self._value
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\services.py", line 585, in <lambda>
    lambda c: self._get_elements(payload=payload_func(c, **kwargs)),
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\services.py", line 89, in _get_elements
    response = self._get_response_xml(payload=payload)
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\services.py", line 163, in _get_response_xml
    allow_redirects=False)
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\util.py", line 547, in post_ratelimited
    _raise_response_errors(r, protocol, log_msg, log_vals)  # Always raises an exception
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\util.py", line 603, in _raise_response_errors
    raise r.headers['TimeoutException']
  File "%WORKSPACE%\nlp\exchangelib\exchangelib\util.py", line 494, in post_ratelimited
    r = session.post(url=url, headers=headers, data=data, allow_redirects=False, timeout=protocol.TIMEOUT)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py", line 522, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\sessions.py", line 628, in send
    r.content
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\models.py", line 755, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File "%USERPROFILE%\AppData\Local\Continuum\Anaconda3\lib\site-packages\requests\models.py", line 679, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: 
    ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", 
    ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
4

0 回答 0