Python requests「Max retries exceeded with url」 error

19-4-29補充:python

  • error1:

NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000038F2B00>: Failed to establish a new connection:
[WinError 10060] 因爲鏈接方在一段時間後沒有正確答覆或鏈接的主機沒有反應,鏈接嘗試失敗。',))git

解決辦法:github

session.keep_alive=Falsejson

  • error2:

python hostname doesn't match either of facebookXXXXX
解決辦法:session

import ssl
ssl.match_hostname = lambda cert, hostname: True
多方查閱後發現瞭解決問題的緣由:http鏈接太多沒有關閉致使的。post

解決辦法:url

  1. 增長重試鏈接次數

    requests.adapters.DEFAULT_RETRIES = 5code

  2. 關閉多餘的鏈接

requests使用了urllib3庫,默認的http connection是keep-alive的,requests設置False關閉。ip

操做方法ssl

s = requests.session()
s.keep_alive = False
  1. 只用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上有人給出了這樣的解釋。

  1. 安裝 py

pip install -U pyopenssl
五、設定固定的睡眠時間在發送請求之間
https://github.com/requests/r...
https://stackoverflow.com/que...

相關文章
相關標籤/搜索