Last active
February 20, 2024 12:05
-
-
Save laixintao/e9eae48a835e741969ae06af3ad45f71 to your computer and use it in GitHub Desktop.
Send HTTP requests using python-requests with timeout, tcp reuse(session) and retry.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sharing my code snippt used to r/w mongo,