ConnectionResetError: [Errno 54]
是一个常见的网络错误,表示在尝试建立或维持网络连接时,连接被对端(服务器)重置。这通常发生在以下几种情况:
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 502, 503, 504, 408),
session=None,
):
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
try:
response = requests_retry_session().get('https://example.com')
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
import requests
from requests.adapters import HTTPAdapter
session = requests.Session()
adapter = HTTPAdapter(pool_connections=100, pool_maxsize=100)
session.mount('http://', adapter)
session.mount('https://', adapter)
try:
response = session.get('https://example.com')
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
import requests
try:
response = requests.get('https://example.com', timeout=(3.05, 27))
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
通过以上方法,可以有效减少或避免ConnectionResetError: [Errno 54]
错误的发生,提高网络请求的稳定性和可靠性。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云