mjs / imapclient

An easy-to-use, Pythonic and complete IMAP client library
https://imapclient.readthedocs.io/
Other
519 stars 85 forks source link

IMAPClient(host="imap.gmail.com") [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076) #410

Closed DonaldSeo1 closed 4 years ago

DonaldSeo1 commented 4 years ago

Hi,

Im using imapclient for connecting to my gmail and parsing email content with bs4.

my script works on my local machine, but when I run it on AWS device farm, it returns the following error

Info about AWS device farm machine platform linux -- Python 3.7.4

common/email_parser.py:12: in parse_email_for with IMAPClient(host="imap.gmail.com") as client: lib/python3.7/site-packages/imapclient/imapclient.py:254: in __init__ self._imap = self._create_IMAP4() lib/python3.7/site-packages/imapclient/imapclient.py:289: in _create_IMAP4 self._timeout) lib/python3.7/site-packages/imapclient/tls.py:44: in __init__ imaplib.IMAP4.__init__(self, host, port) /usr/local/lib/python3.7/imaplib.py:198: in __init__ self.open(host, port) lib/python3.7/site-packages/imapclient/tls.py:50: in open self.sock = wrap_socket(sock, self.ssl_context, host) lib/python3.7/site-packages/imapclient/tls.py:32: in wrap_socket return ssl_context.wrap_socket(sock, server_hostname=host) /usr/local/lib/python3.7/ssl.py:423: in wrap_socket session=session /usr/local/lib/python3.7/ssl.py:870: in _create self.do_handshake() `self = <ssl.SSLSocket [closed] fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6> block = False

@_sslcopydoc
def do_handshake(self, block=False):
    self._check_connected()
    timeout = self.gettimeout()
    try:
        if timeout == 0.0 and block:
            self.settimeout(None)
      self._sslobj.do_handshake()

E ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

/usr/local/lib/python3.7/ssl.py:1139: SSLCertVerificationError`

I've tried passing in ssl=False to see if that solve the issue but it did not.

Thank you for reading this post.

DonaldSeo1 commented 4 years ago

btw this is part of the Appium Automation test script that I am trying to run in AWS device farm (email parsing is part of the task that's required in Appium automation script)

using 2.1.0 version of imapclient Screen Shot 2020-09-14 at 4 16 35 PM

mjs commented 4 years ago

It's likely that the standard root certificate store isn't installed in the cloud environment you're using.

If the AWS instances you're using are based on Ubuntu you can install these root certificates by installing the ca-certificates package (apt install ca-certificates). Other operating systems make these root certs available as a standard package too (the name varies).

If that isn't possible for some reason, you can also use the certfi package to make the standard root certificates available to your program. See the IMAPClient docs for more details: https://imapclient.readthedocs.io/en/2.1.0/concepts.html#tls-ssl

If none of the above works you can turn off certificate verification by passing a custom SSLContext but I really recommend against that. See the docs in the standard library: https://docs.python.org/3/library/ssl.html#ssl.SSLContext

Closing this as this issue is outside of IMAPClient's scope.