我到了 urllib2.URLError: 调用mechanize.browser.open('我的https网站')时出错。
我在网上搜索但没有任何对我有用。
这是我的代码:
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")
以下是我遇到的错误:
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
其他信息:
import ssl
print ssl.OPENSSL_VERSION
output>> OpenSSL 1.0.2j 26 Sep 2016
python版
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
有没有办法绕过这个错误?
注意:
答案 0 :(得分:2)
此网站的问题不是证书验证,因为您已成功将其关闭。问题是该站点仅支持不再被认为是安全的密码,即基于3DES和RC4的密码。 出于安全原因,ssl库中的默认密码不包含这些密码。
要添加对这些密码的支持,您可以手动设置默认密码集。以下行将DES-CBC3-SHA
设置为提供的密码。这样您就可以访问损坏的网站:
ssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA')
myBrowser = mechanize.Browser()
...
请注意,您只应将此设置用于特定网站。虽然理论上也可以为_DEFAULT_CIPHERS设置一个更大的密码集来处理所有站点,但是这个特定站点还有其他问题:即使DES-CBC3-SHA包含在内,它看起来也会因TLS握手而失败提供的密码集,但是如果在DES-CBC3-SHA之前提供更新的密码(如GCM)。