LoginSignup
1
2

More than 1 year has passed since last update.

Zscaler環境でPythonが証明書を利用してhttpsのURLへアクセスする方法

Last updated at Posted at 2023-04-12

ステップ1: 証明書の取得

Zscaler環境でhttpsのURLにアクセスするためには、Zscalerのルート証明書を取得する必要があります。Zscalerのルート証明書は、Zscalerの公式ウェブサイトからダウンロードすることができます。

ステップ2: Pythonのリクエストの送信

ステータス:証明書のインポートとプロキシの設定が完了したら、Pythonのリクエストを送信

import requests
import ssl
import certifi

# 証明書のインポートと検証の有効化
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH, cafile=certifi.where())
ssl_ctx.load_cert_chain(cert_file)

# プロキシの設定
proxy = {
  'http': 'http://proxy.xxx.com:8000',
  'https': 'http://proxy.xxx.com:8000',
}

# httpsのURLにアクセス
response = requests.get('https://xxx.com', proxies=proxy, verify=ssl_ctx)

# レスポンスの内容を表示
print(response.content)

Tips:
うまくいけない場合
証明書を変数REQUESTS_CA_BUNDLEに設定すれば、Pythonコード上証明書の指定は不要になります。
linux:
echo "export REQUESTS_CA_BUNDLE=/ca-bundle.pem" >> $HOME/.bashrc
Windows:
[System.Environment]::SetEnvironmentVariable("REQUESTS_CA_BUNDLE", "\ca-bundle.pem", "Machine")

1
2
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
1
2