Jainmiah
Jainmiah

Reputation: 467

ssl.CertificateError: hostname 'example.com' doesn't match either of 'exampleserver.com'?

when I'm trying open a website with urllib library I'm getting the error. I'm not getting why this error occurs? currently I'm using python 3.6 version. Is this problem with version?

url = 'https://example.com'
html = urllib.request.urlopen(url).read().decode('utf-8')
text = get_text(html)
data = text.split()
print(data)

Upvotes: 0

Views: 7764

Answers (2)

Jainmiah
Jainmiah

Reputation: 467

I have solved it by this code:

need to import ssl library

import ssl

need to add this code before loading url

ssl.match_hostname = lambda cert, hostname: True

Example:

import ssl

ssl.match_hostname = lambda cert, hostname: True
url = 'https://example.com'
html = urllib.request.urlopen(url).read().decode('utf-8')
text = get_text(html)
data = text.split()
print(data)

Upvotes: 2

ajay gandhi
ajay gandhi

Reputation: 563

This error occurs when the common name of your SSL/TLS Certificate does not match the domain name of you site. Try changing the domain name of your site to exampleServer.Com and see if it works. Otherwise, you will have to write custom logic to ignore name mismatch error.

Upvotes: 1

Related Questions