HTTPS Request SSL Certificate validation

Source: Internet
Author: User
Tags ssl certificate

Import Urllib2url = "https://www.12306.cn/mormhweb/" headers = {"User-agent": "mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/54.0.2840.99 safari/537.36 "}request = urllib2. Request (URL, headers = headers) Response = Urllib2.urlopen (request) print response.read ()

  

Operation Result:

urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>

Therefore, if you encounter such a site in the future, we need to handle the SSL certificate separately, let the program ignore the SSL certificate validation error, can be accessed normally.

Import Urllibimport urllib2# 1. Import Python SSL processing module import ssl# 2. Indicates ignoring unverified SSL certificate authentication context = ssl._create_unverified_context () url = "https://www.12306.cn/mormhweb/" headers = {" User-agent ":" mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/54.0.2840.99 safari/537.36 "}request = urllib2. Request (URL, headers = headers) # 3. Specify the Add context parameter in the Urlopen () method response = Urllib2.urlopen (Request, context = context) Print Response.read ()

  

HTTPS Request SSL Certificate validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.