LoginSignup
0
0

More than 1 year has passed since last update.

【備忘録】HTTPリクエストのテスト

Last updated at Posted at 2023-02-24

下記URLに検証したいステータスコード,etcを付けてリクエスト

以下429エラーの場合のサンプルコード

sample.py
import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

session = requests.Session()
url = "https://httpstat.us/429"

retry_strategy = Retry(
    total=2,
    backoff_factor=1,
    status_forcelist=[
        429,
    ],
)
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
session.mount("http://", HTTPAdapter(max_retries=retry_strategy))

res = session.request(
    "GET",
    url,
    timeout=10,
)

if res.status_code == 429:
    retry_after = retry_strategy.get_retry_after(res)
    print(retry_after)
0
0
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
0