Sujay
Sujay

Reputation: 94

Bypass SSL certificate in Python using urllib

I am trying to bypass the SSL Certificate and use the URL in Python, All HTTPS sites are getting the same error

Kindly suggest how can it get resolved. Thanks in Advance.

Code:

import urllib.request as ur
import urllib.parse, urllib.error, ssl


url_is = 'https://finance.yahoo.com'
url_google = 'https://www.google.co.in'

req = ur.Request(url_google)

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

info = ur.urlopen(req, context=ctx).read()
Message.Chat.SendMessage ("" + info)

Error:

Traceback (most recent call last):
  File "/usr/lib/python3.8/urllib/request.py", line 1354, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/usr/lib/python3.8/http/client.py", line 1256, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1302, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1251, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1011, in _send_output
    self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 951, in send
    self.connect()
  File "/usr/lib/python3.8/http/client.py", line 1418, in connect
    super().connect()
  File "/usr/lib/python3.8/http/client.py", line 922, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.8/socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    info = ur.urlopen(req, context=gcontext).read()
  File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.8/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/lib/python3.8/urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.8/urllib/request.py", line 1397, in https_open
    return self.do_open(http.client.HTTPSConnection, req,
  File "/usr/lib/python3.8/urllib/request.py", line 1357, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>

Example is available at https://onlinegdb.com/rdj-HSEFAz

I have also tried with the below code but the same issue.

from bs4 import BeautifulSoup
import urllib.request as ur
import urllib.parse, urllib.error, ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE


url_is = 'https://finance.yahoo.com'
url_google = 'https://www.google.co.in'

read_data = urllib.request.urlopen(url_google, context=ctx).read() 
soup_is= BeautifulSoup(read_data,'lxml')

Upvotes: 1

Views: 1406

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123280

Temporary failure in name resolution

This means that no IP address for the given hostname can be found. This is completely unrelated to SSL and thus no "bypass SSL certificate" will help.

The problem is instead that DNS does not properly work in the software environment where this code is run. This needs to be fixed in this environment (no details given, so no help can be provided) and not in the code.

I have tested that with a working DNS the code works fine.

Upvotes: 2

Related Questions