爬取https頁面遇到“SSLError: hostname 'xxx' doesn't match either of”的解決方法


 

  使用python requests 框架包訪問https://itunes.apple.com 頁面是遇到 SSLError: hostname 'itunes.apple.com' doesn't match either of 錯誤,

但本地開發時沒報錯,而是在線上報錯,python版本是2.7.3,后來gg到是因為版本問題,安裝好pyOpenSSLndg-httpsclientpyasn1 即可,發現確實可行。

These errors occur when SSL certificate verification fails to match the certificate the server responds with to the hostname Requests thinks it's contacting. If you're certain the server's SSL setup is correct (for example, because you can visit the site with your browser) and you're using Python 2.6 or 2.7, a possible explanation is that you need Server-Name-Indication.

Server-Name-Indication, or SNI, is an official extension to SSL where the client tells the server what hostname it is contacting. This is important when servers are using Virtual Hosting. When such servers are hosting more than one SSL site they need to be able to return the appropriate certificate based on the hostname the client is connecting to.

Python3 and Python 2.7.9+ include native support for SNI in their SSL modules. For information on using SNI with Requests on Python < 2.7.9 refer to this Stack Overflow answer.

 

The current version of Requests should be just fine with SNI. Further down the GitHub issue you can see the requirements:

Try installing those packages and then give it another shot.

 

參考資料:

1、http://docs.python-requests.org/en/master/community/faq/#what-are-hostname-doesn-t-match-errors

2、  https://stackoverflow.com/questions/18578439/using-requests-with-tls-doesnt-give-sni-support/18579484#18579484

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM