I am getting urllib2.URLError: error while calling mechanize.browser.open('my https site').
I searched the web but nothing worked for me.
Here is my code:
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
import mechanize
import operator
from bs4 import BeautifulSoup
import os
myBrowser = mechanize.Browser()
myBrowser.set_handle_robots(False)
myBrowser.set_handle_refresh(False)
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
Here is the error I am getting:
Traceback (most recent call last):
File "C:/Users/Himanshu/Desktop/UIET Rank system.py", line 27, in <module>
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 254, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 284, in _mech_open
response = UserAgentBase.open(self, request, data)
File "C:\Python27\lib\site-packages\mechanize\_opener.py", line 195, in open
response = urlopen(self, req, data)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 352, in _open
'_open', req)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 340, in _call_chain
result = func(*args)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1215, in https_open
return self.do_open(conn_factory, req)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1160, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:661)>
Process finished with exit code 1
Other information:
import ssl
print ssl.OPENSSL_VERSION
output>> OpenSSL 1.0.2j 26 Sep 2016
python version
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Is there any way to bypass this error?
Note:
The problem with this site is not the certificate validation since you have successfully switched it off. The problem instead is that the site only supports ciphers which are no longer considered secure, i.e. 3DES and RC4 based ciphers. The default ciphers in the ssl library don't include these ciphers for security reasons.
To add support for these ciphers you can manually set the default cipher set. The following line sets DES-CBC3-SHA
as the offered cipher. This way you can access the broken site:
ssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA')
myBrowser = mechanize.Browser()
...
Note that you should use this setting only for the specific site. While it might in theory be also possible to just set a larger cipher set for _DEFAULT_CIPHERS to handle all sites, this specific site suffers from additional problems: it looks like it will fail with the TLS handshake even if DES-CBC3-SHA is included in the offered cipher set but if newer ciphers (like GCM) are offered before DES-CBC3-SHA.