ML engineer/Papers & CS generals

[였λ₯˜ν•΄κ²°] osX + python3 + SSL certificate Error

naubull2 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]

 

λ°˜μ‘ν˜•