个人博客导航页(点击右侧链接即可打开个人博客):大牛带你入门技术栈
一句话解决方案
import requests
from requests.adapters import HTTPAdapter
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=3))
s.mount('https://', HTTPAdapter(max_retries=3))
s.get('http://example.com', timeout=5)
另外一种方式,深度定制, 但是我尝试了在python2.7的版本下不可以, 因为max_retries传入的参数是整型的, 如果传入Retry对象的话, 会报错:total -= 1 TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
def retry_test():
s = requests.Session()
retries = Retry(total=5,
backoff_factor=6,
status_forcelist=[500, 502, 503, 504])
s.mount('http://', HTTPAdapter(max_retries=retries))
resp = s.get('http://localhost:8088', timeout=3)
print resp.text
原理
urllib3.connectionpool.py里, urlopen通过递归的方式实现
def urlopen(
self,
method,
url,
body=None,
headers=None,
retries=None,
redirect=True,
assert_same_host=True,
timeout=_Default,
pool_timeout=None,
release_conn=None,
chunked=False,
body_pos=None,
**response_kw
)
"""remove some codes"""
try:
# Request a connection from the queue.
timeout_obj = self._get_timeout(timeout)
conn = self._get_conn(timeout=pool_timeout)
conn.timeout = timeout_obj.connect_timeout
is_new_proxy_conn = self.proxy is not None and not getattr(
conn, "sock", None
)
if is_new_proxy_conn:
self._prepare_proxy(conn)
# Make the request on the httplib connection object.
httplib_response = self._make_request(
conn,
method,
url,
timeout=timeout_obj,