Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mechanize : SSL: CERTIFICATE_VERIFY_FAILED

Well I was trying to create a auto login script for my hostel wifi, I need to login to wifi for getting network access. The webpage stays open on local network if I am connected to wifi. I just have to visit the webpage and login there. So I was trying to use mechanize to login.

Here is python code.

I was just trying to print the from for now.

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.open("https://192.168.10.3/connect/PortalMain")
for f in br.forms():
    print f

The error I am getting is

Traceback (most recent call last):
  File "demo.py", line 4, in <module>
    br.open("https://192.168.10.3/connect/PortalMain")
  File "/usr/local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 254, in open
    return self._mech_open(url_or_request, data, timeout=timeout)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_mechanize.py", line 284, in _mech_open
    response = UserAgentBase.open(self, request, data)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_opener.py", line 195, in open
    response = urlopen(self, req, data)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 352, in _open
    '_open', req)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 340, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 1215, in https_open
    return self.do_open(conn_factory, req)
  File "/usr/local/lib/python2.7/site-packages/mechanize/_urllib2_fork.py", line 1160, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)>
like image 855
Parth Manaktala Avatar asked Jan 03 '23 09:01

Parth Manaktala


1 Answers

The site is probably using a self signed SSL certificate, you can turn off SSL verification (although you should understand the security risks first), add this code before br.open():

import ssl
try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    # Legacy Python that doesn't verify HTTPS certificates by default
    pass
else:
    # Handle target environment that doesn't support HTTPS verification
    ssl._create_default_https_context = _create_unverified_https_context

Source: Disable ssl certificate validation in mechanize

So in your case the code would end up being:

import mechanize
import ssl

ssl._create_default_https_context = ssl._create_unverified_context
br = mechanize.Browser()
br.set_handle_robots(False)
br.open("https://192.168.10.3/connect/PortalMain")
for f in br.forms():
    print f

Should work but I've not tested it.

like image 139
xxx Avatar answered Jan 04 '23 21:01

xxx