2018-04-25 Python requests“Max retries exceeded with url” error

19-4-29补充:

error1:

NewConnectionError(': Failed to establish a new connection:
 [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))

解决办法:

session.keep_alive=False

error2:

python hostname doesn't match either of  facebookXXXXX

解决办法:

import ssl
ssl.match_hostname = lambda cert, hostname: True

多方查阅后发现了解决问题的原因:http连接太多没有关闭导致的。

解决办法:

1、增加重试连接次数

  requests.adapters.DEFAULT_RETRIES = 5

2、关闭多余的连接

requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。

操作方法

s = requests.session()
s.keep_alive = False

3、只用session进行操作。即只创建一个连接,并设置最大连接数或者重试次数。

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry


session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

session.get(url)
  import requests
    from requests.adapters import HTTPAdapter
    from requests.packages.urllib3.util.retry import Retry
    s = requests.Session()
    retry = Retry(connect = 5, backoff_factor = 1)
    adapter = HTTPAdapter(max_retries = retry)
    s.mount('http://', adapter)
    s.keep_alive = False
    res = s.post(self.conn.host + '/sign-in', data = json.dumps({
        'name': "XXX",
        'pwd': "XXX"
    }))
    response = res.json()

但是在starkoverflow上有人给出了这样的解释。

4.安装 py

pip install -U pyopenssl

5、设定固定的睡眠时间在发送请求之间
https://github.com/requests/requests/issues/4246#event
https://stackoverflow.com/questions/23013220/max-retries-exceeded-with-url

你可能感兴趣的:(2018-04-25 Python requests“Max retries exceeded with url” error)