爬取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://*.com/questions/18578439/using-requests-with-tls-doesnt-give-sni-support/18579484#18579484

上一篇:sigaction 的使用


下一篇:array_intersect() php筛选两个数组共有的元素