ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [์˜ค๋ฅ˜ํ•ด๊ฒฐ] osX + python3 + SSL certificate Error
    ML engineer/Papers & CS generals 2023. 1. 7. 20:42
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ•“ 3 mins read

    # SSLCertVerificationError

    osX + python3.X ์—์„œ ์ผ๋ถ€ ๋ชจ๋“ˆ ๋‚ด์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ฑฐ๋‚˜ ํ•˜๋Š” ๊ฒฝ์šฐ์— ๊ฐ€๋” ๋ณผ ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.

    SSL ์ธ์ฆ์„œ ์˜ค๋ฅ˜๋ฌธ์ œ์ธ๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด python3.X ์—์„œ, ํŒŒ์ด์ฌ ๋ชจ๋“ˆ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๋Š” ๊ฒฝ์šฐ์— ์ฃผ๋กœ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
    (๋ฌผ๋ก  SSL ์ธ์ฆ์„œ๋ฅผ ์š”๊ตฌํ•˜๋Š” ํ†ต์‹ ์ด๋ผ๋ฉด ๊ผญ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์šด ๋ฐ›์„๋•Œ๊ฐ€ ์•„๋‹์ˆ˜๋„ ์žˆ๊ฒ ์ฃ ?)

    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1350, in do_open
        encode_chunked=req.has_header('Transfer-encoding'))
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1277, in request
        self._send_request(method, url, body, headers, encode_chunked)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1323, in _send_request
        self.endheaders(body, encode_chunked=encode_chunked)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1272, in endheaders
        self._send_output(message_body, encode_chunked=encode_chunked)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1032, in _send_output
        self.send(msg)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 972, in send
        self.connect()
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1447, in connect
        server_hostname=server_hostname)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 423, in wrap_socket
        session=session
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 870, in _create
        self.do_handshake()
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1139, in do_handshake
        self._sslobj.do_handshake()
    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)

    - ์œ„์˜ ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ ๋งฅ๋ถ์—์„œ korpora๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํ•œ๊ตญ์–ด ์ฝ”ํผ์Šค๋ฅผ ๋‹ค์šด ๋ฐ›์„๋•Œ ๋ฐœ์ƒํ•œ ๋กœ๊ทธ์ธ๋ฐ์š”,

    # Solution

    "Finder"์—์„œ "Applications/python3.X/Install Certificates.command" ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ง์ ‘ ์‹คํ–‰ ํ•ด์ฃผ๊ฑฐ๋‚˜,


    ์•„๋ž˜์˜ bash ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
    (๊ฐ™์€ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, python3.X์˜ ํ•ด๋‹น ๊ฒฝ๋กœ์— ๋งž๊ฒŒ ์ˆ˜์ • ํ•„์š”)

    #!/bin/sh
    
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 << "EOF"
    
    # install_certifi.py
    #
    # sample script to install or update a set of default Root Certificates
    # for the ssl module.  Uses the certificates provided by the certifi package:
    #       https://pypi.org/project/certifi/
    
    import os
    import os.path
    import ssl
    import stat
    import subprocess
    import sys
    
    STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
                 | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
                 | stat.S_IROTH |                stat.S_IXOTH )
    
    def main():
        openssl_dir, openssl_cafile = os.path.split(
            ssl.get_default_verify_paths().openssl_cafile)
    
        print(" -- pip install --upgrade certifi")
        subprocess.check_call([sys.executable,
            "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
    
        import certifi
    
        # change working directory to the default SSL directory
        os.chdir(openssl_dir)
        relpath_to_certifi_cafile = os.path.relpath(certifi.where())
        print(" -- removing any existing file or link")
        try:
            os.remove(openssl_cafile)
        except FileNotFoundError:
            pass
        print(" -- creating symlink to certifi certificate bundle")
        os.symlink(relpath_to_certifi_cafile, openssl_cafile)
        print(" -- setting permissions")
        os.chmod(openssl_cafile, STAT_0o775)
        print(" -- update complete")
    
    if __name__ == '__main__':
        main()
    EOF

     

    SSL ์ธ์ฆ์„œ ์„ค์น˜ ํ›„์—” ์ฝ”ํผ์Šค ๋‹ค์šด๋กœ๋“œ๋„ ์ž˜ ์ž‘๋™ํ•˜๋Š” ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

    (korpora).venv โฏ korpora fetch --corpus korean_petitions
    ## Arguments of Korpora CLI ##
      - corpus : ['korean_petitions']
      - force_download : False
      - func : fetch
      - root : None
    [korean_petitions] download petitions_2017-08: 1.84MB [00:00, 7.06MB/s]
    [korean_petitions] download petitions_2017-09: 20.4MB [00:01, 11.0MB/s]
    [korean_petitions] download petitions_2017-10: 12.0MB [00:00, 20.6MB/s]
    [korean_petitions] download petitions_2017-11: 28.4MB [00:01, 27.6MB/s]
    [korean_petitions] download petitions_2017-12: 29.0MB [00:00, 29.8MB/s]
    [korean_petitions] download petitions_2018-01: 43.9MB [00:01, 29.7MB/s]
    [korean_petitions] download petitions_2018-02: 33.8MB [00:01, 31.6MB/s]
    [korean_petitions] download petitions_2018-03: 34.3MB [00:01, 22.0MB/s]
    [korean_petitions] download petitions_2018-04: 35.5MB [00:01, 30.5MB/s]
    [korean_petitions] download petitions_2018-05: 37.5MB [00:01, 24.1MB/s]
    [korean_petitions] download petitions_2018-06: 37.8MB [00:01, 32.5MB/s]
    [korean_petitions] download petitions_2018-07: 40.5MB [00:01, 33.1MB/s]
    [korean_petitions] download petitions_2018-08: 39.8MB [00:01, 32.1MB/s]
    [korean_petitions] download petitions_2018-09: 36.1MB [00:01, 29.6MB/s]
    [korean_petitions] download petitions_2018-10: 38.1MB [00:01, 29.6MB/s]
    [korean_petitions] download petitions_2018-11: 37.7MB [00:01, 31.7MB/s]
    [korean_petitions] download petitions_2018-12: 33.0MB [00:00, 34.5MB/s]
    [korean_petitions] download petitions_2019-01: 34.8MB [00:02, 13.6MB/s]
    [korean_petitions] download petitions_2019-02: 30.8MB [00:01, 19.2MB/s]
    [korean_petitions] download petitions_2019-03: 34.9MB [00:01, 28.8MB/s]

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by naubull2.