Skip to content

Instantly share code, notes, and snippets.

@wupher
Forked from laixintao/decent_request.py
Created December 27, 2023 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wupher/317544b7a420f1c8e6aed7c8b50f2f57 to your computer and use it in GitHub Desktop.
Save wupher/317544b7a420f1c8e6aed7c8b50f2f57 to your computer and use it in GitHub Desktop.
Send HTTP requests using python-requests with timeout, tcp reuse(session) and retry.
from requests.adapters import HTTPAdapter, Retry
from requests import Session
retries = Retry(
total=5, backoff_factor=1, status_forcelist=[502, 503, 504]
)
session = Session() # reuse tcp connection
session.mount("http://", HTTPAdapter(max_retries=retries))
session.mount("https://", HTTPAdapter(max_retries=retries))
session.get("https://example.com", timeout=5) # seconds
@wupher
Copy link
Author

wupher commented Dec 27, 2023

防止 python http request 出现hang-up 。不过,也可以简单使用 httpx lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment