Skip to content

Instantly share code, notes, and snippets.

@laixintao
Last active February 20, 2024 12:05
Show Gist options
  • Save laixintao/e9eae48a835e741969ae06af3ad45f71 to your computer and use it in GitHub Desktop.
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.
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
@QuantumGhost
Copy link

QuantumGhost commented Dec 22, 2023

以及, 如 https://t.me/laiskynotes/100 这条提到的, 如果你请求的是个很慢的服务器 (比如每 3s 给你返回一个字节),
那实际的请求时间可能远大于 timeout

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