1. 程式人生 > >Caused by SSLError

Caused by SSLError

 1 import requests
 2 from bs4 import BeautifulSoup
 3 
 4 
 5 def get_page():
 6     url = 'https://movie.douban.com/cinema/nowplaying/changsha/'
 7     headers = {
 8         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
 9     }
10 response = requests.get(url, headers=headers) 11 text = response.text 12 return text 13 29 30 if __name__ == '__main__': 31 text = get_page() 32

程式執行報錯:requests.exceptions.SSLError: HTTPSConnectionPool(host='movie.douban.com', port=443): Max retries exceeded with url: /cinema/nowplaying/changsha/ (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

其實很奇怪,我昨天晚上的時候程式碼還正常運行了,今天竟然就報錯了,百度找到一個解決方法,原文內容:

python locust介面效能測試HTTPS網站報錯:Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certi

2017年12月13日 18:38:39  閱讀數:4115 個人分類: python 學習  

問題描述:

測試HTTPS SSL 協議的網站介面,用Python Locust模組,不論POST還是GET都提示錯誤:

SSLError
Max retries exceeded with url: /action.php?m=upload 
(Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))",),)

 

後面查官網locust.io,終於找到解決方法:https://docs.locust.io/en/latest/api.html

 

request (method, url, name=None, catch_response=False, **kwargs)

Constructs and sends a requests.Request. Returns requests.Response object.

Parameters:
  • method – method for the new Request object.
  • url – URL for the new Request object.
  • name – (optional) An argument that can be specified to use as label in Locust’s statistics instead of the URL path. This can be used to group different URL’s that are requested into a single entry in Locust’s statistics.
  • catch_response – (optional) Boolean argument that, if set, can be used to make a request return a context manager to work as argument to a with statement. This will allow the request to be marked as a fail based on the content of the response, even if the response code is ok (2xx). The opposite also works, one can use catch_response to catch a request and then mark it as successful even if the response code was not (i.e 500 or 404).
  • params – (optional) Dictionary or bytes to be sent in the query string for the Request.
  • data – (optional) Dictionary or bytes to send in the body of the Request.
  • headers – (optional) Dictionary of HTTP Headers to send with the Request.
  • cookies – (optional) Dict or CookieJar object to send with the Request.
  • files – (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
  • auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.
  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
  • allow_redirects (bool) – (optional) Set to True by default.
  • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
  • stream – (optional) whether to immediately download the response content. Defaults to False.
  • verify – (optional) if True, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
  • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
利用verify引數,直接在POST/GET請求新增verify = False引數,搞定,

 

這是HTTPS網站所做的SSL證書認證,預設是True的,設定為False之後對測試沒有影響;

當然如果知道證書的路徑,應該也可以用cert設定.pem檔案的路徑,也可以解決

######################################################################################################################################### 在程式碼第10行更改程式碼為
response = requests.get(url, headers=headers, verify=False),程式可以正常執行。
另外找到其他的解決方法,可是才剛開始學,不知道怎麼使用。
另外的解決方法連結https://www.cnblogs.com/lykbk/p/ASDFQAWQWEQWEQWEQWEQWEQWEQEWEQW.html
https://blog.csdn.net/wangxiaotian2007/article/details/79284124