Python:在 mechanize 中打开 link 时 self._sslobj.do_handshake() 出错
Python: Error in self._sslobj.do_handshake() during opening a link in mechanize
我正在尝试重新运行大约 2 年前编写的代码,但现在它不起作用。我正在使用 python 3.7.1。
导致问题的代码片段如下
import ssl
import mechanize
import operator
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
ssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA')
myBrowser = mechanize.Browser()
myBrowser.set_handle_robots(False)
myBrowser.set_handle_refresh(False)
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
这导致 ssl 握手期间出现异常,并且仅显示 [错误 0]。完整的追溯如下-
Traceback (most recent call last):
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1179, in do_open
headers)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1016, in _send_output
self.send(msg)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 956, in send
self.connect()
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1392, in connect
server_hostname=server_hostname)
File "/home/him/anaconda3/lib/python3.7/ssl.py", line 412, in wrap_socket
session=session
File "/home/him/anaconda3/lib/python3.7/ssl.py", line 853, in _create
self.do_handshake()
File "/home/him/anaconda3/lib/python3.7/ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "UIET Rank system.py", line 51, in <module>
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_mechanize.py", line 253, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_mechanize.py", line 283, in _mech_open
response = UserAgentBase.open(self, request, data)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_opener.py", line 193, in open
response = urlopen(self, req, data)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 375, in _open
'_open', req)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 363, in _call_chain
result = func(*args)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1225, in https_open
return self.do_open(conn_factory, req)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1182, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 0] Error>
你能解释一下实际问题是什么以及如何解决吗?
注意- 请 Visit this link 我正在打开并检查它,它可能使用过时的密码并且在继续之前总是显示警告。
虽然还不完全清楚您将哪个版本的 OpenSSL 与 Python 一起使用,但我的猜测是它是 OpenSSL 1.1.1,因为它随 Anaconda Python 3.7 一起提供。这个版本不再支持 DES-CBC3-SHA,即这个密码被认为是弱的并且没有被编译。这就是为什么 ssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA')
本质上将支持的密码列表设置为 none 这使得 TLS 握手失败。
如果您需要此弱密码,则需要返回到旧版本的 OpenSSL 或重新编译 OpenSSL 并Python明确启用此弱密码支持。
我正在尝试重新运行大约 2 年前编写的代码,但现在它不起作用。我正在使用 python 3.7.1。 导致问题的代码片段如下
import ssl
import mechanize
import operator
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
ssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA')
myBrowser = mechanize.Browser()
myBrowser.set_handle_robots(False)
myBrowser.set_handle_refresh(False)
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
这导致 ssl 握手期间出现异常,并且仅显示 [错误 0]。完整的追溯如下-
Traceback (most recent call last):
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1179, in do_open
headers)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1016, in _send_output
self.send(msg)
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 956, in send
self.connect()
File "/home/him/anaconda3/lib/python3.7/http/client.py", line 1392, in connect
server_hostname=server_hostname)
File "/home/him/anaconda3/lib/python3.7/ssl.py", line 412, in wrap_socket
session=session
File "/home/him/anaconda3/lib/python3.7/ssl.py", line 853, in _create
self.do_handshake()
File "/home/him/anaconda3/lib/python3.7/ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "UIET Rank system.py", line 51, in <module>
myBrowser.open("https://uwp.puchd.ac.in/common/viewmarks.aspx")
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_mechanize.py", line 253, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_mechanize.py", line 283, in _mech_open
response = UserAgentBase.open(self, request, data)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_opener.py", line 193, in open
response = urlopen(self, req, data)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 375, in _open
'_open', req)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 363, in _call_chain
result = func(*args)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1225, in https_open
return self.do_open(conn_factory, req)
File "/home/him/.local/lib/python3.7/site-packages/mechanize/_urllib2_fork.py", line 1182, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 0] Error>
你能解释一下实际问题是什么以及如何解决吗?
注意- 请 Visit this link 我正在打开并检查它,它可能使用过时的密码并且在继续之前总是显示警告。
虽然还不完全清楚您将哪个版本的 OpenSSL 与 Python 一起使用,但我的猜测是它是 OpenSSL 1.1.1,因为它随 Anaconda Python 3.7 一起提供。这个版本不再支持 DES-CBC3-SHA,即这个密码被认为是弱的并且没有被编译。这就是为什么 ssl._DEFAULT_CIPHERS = ('DES-CBC3-SHA')
本质上将支持的密码列表设置为 none 这使得 TLS 握手失败。
如果您需要此弱密码,则需要返回到旧版本的 OpenSSL 或重新编译 OpenSSL 并Python明确启用此弱密码支持。