0
1

Pythonのrequestsライブラリが読み込んでいるrootCA証明書の場所を確認するワンライナー

Posted at

はじめに

プロキシ環境下などにおいて、独自のrootCA証明書を読み込ませずにPythonでrequestsを行うとSSL証明書エラーが発生する。

requestsが読み込んでいる証明書に独自のrootCA証明書を追記すれば解決できるのだが、毎回読み込んでいる証明書の場所と確認方法を忘れてしまうので、自分向けの備忘として残しておく。

環境

Python 3.11.2

読み込んでいる証明書のパスを確認するワンライナー

pythonコマンドにcオプションをつけると、囲った中でコード実行ができる。

$ python -c "import requests; print(requests.certs.where())"

# /usr/local/lib/python3.11/site-packages/certifi/cacert.pem ※Linuxはこんな感じ
# C:\Users\<ユーザー名>\AppData\Local\Programs\Python\Python311\Lib\site-packages\certifi\cacert.pem ※windowsはこんな感じ

requestsの証明書は依存パッケージであるcertifi内のcacert.pemで管理されている。

requestsのソースを見るとわかるが、上記コードはcertifiを呼び出しているだけだったりする。

大元は以下のコード

Usage

To reference the installed certificate authority (CA) bundle, you can use the built-in function:

>>> import certifi

>>> certifi.where()
'/usr/local/lib/python3.7/site-packages/certifi/cacert.pem'

したがって、以下のようなワンライナーも可能(普通は実行しないが)。

$ python -c "import certifi; print(certifi.where())"

おわりに

記事にしてみると単純にメモするだけでなくて一定根拠も調査する必要があるため、知識が拾えてよかった。

cオプションでコード実行ができることも、覚えておくと役立つ気がした。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1