英文:
Mocking an SSLError in Python
问题
I've been dealing with the ssl.SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled
issue described here. As part of the fix, I'm attempting to write in exception-handling for the exception:
try:
return req()
except (ssl.SSLError, RSSLError) as ssl_err:
print(ssl_err)
if "UNSAFE_LEGACY_RENEGOTIATION_DISABLED" in str(ssl_err):
ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ctx.options |= 0x4
self._sess.mount("https://", CustomHttpAdapter(ctx))
return req()
raise
The issue I'm having is testing it. I've tried doing this:
err = SSLError()
err.reason = "UNSAFE_LEGACY_RENEGOTIATION_DISABLED"
but this prints as ()
. How do I create a mock SSLError
that I can use to test this code?
英文:
I've been dealing with the ssl.SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled
issue described here. As part of the fix, I'm attempting to write in exception-handling for the exception:
try:
return req()
except (ssl.SSLError, RSSLError) as ssl_err:
print(ssl_err)
if "UNSAFE_LEGACY_RENEGOTIATION_DISABLED" in str(ssl_err):
ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ctx.options |= 0x4
self._sess.mount("https://", CustomHttpAdapter(ctx))
return req()
raise
The issue I'm having is testing it. I've tried doing this:
err = SSLError()
err.reason = "UNSAFE_LEGACY_RENEGOTIATION_DISABLED"
but this prints as ()
. How do I create a mock SSLError
that I can use to test this code?
答案1
得分: 1
你需要将该字符串作为构造函数的参数传递:
>>> from ssl import SSLError
>>> error = SSLError("UNSAFE_LEGACY_RENEGOTIATION_DISABLED")
>>> print(error)
('UNSAFE_LEGACY_RENEGOTIATION_DISABLED',)
英文:
You need to pass the string as your argument to the constructor:
>>> from ssl import SSLError
>>> error = SSLError("UNSAFE_LEGACY_RENEGOTIATION_DISABLED")
>>> print(error)
('UNSAFE_LEGACY_RENEGOTIATION_DISABLED',)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论