python-mechanize/mechanize

Error to open URL due certificate invalid

RafaelRViana opened this issue · 5 comments

Hi,

I could not open a URL that certificate is invalid. How could I bypass the certificate? (In browser I can open the page when I add an exception to this certificate)

I get this error:
urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:1056)>

I've already tried this solution:https://stackoverflow.com/questions/30551400/disable-ssl-certificate-validation-in-mechanize
But didn't work for me.

Code:

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)

url = 'https://receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaSituacao/ConsultaPublica.asp'
br.open(url)

Hi,

Even with that line of code:
br.set_ca_data(context=ssl._create_unverified_context(cert_reqs=ssl.CERT_NONE))

I continue with the same error.

br.open(url)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_mechanize.py", line 253, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_mechanize.py", line 283, in _mech_open
response = UserAgentBase.open(self, request, data)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_opener.py", line 193, in open
response = urlopen(self, req, data)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 368, in _open
'_open', req)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 356, in _call_chain
result = func(*args)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1214, in https_open
return self.do_open(conn_factory, req)
File "/Users/rafaelviana/.virtualenvs/py3cv4/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1171, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:1056)>

Because that is not a certificate validation error, but a protocol error.

Is there anything I could do to access this page trough mechanize?

no, that error is coming from python's ssl module, which means you wont be able to open it in any python library.

Ok, thanks for you quick feedback.