模拟 Python 中的 SSLError

huangapple go评论128阅读模式
英文:

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:

  1. try:
  2. return req()
  3. except (ssl.SSLError, RSSLError) as ssl_err:
  4. print(ssl_err)
  5. if "UNSAFE_LEGACY_RENEGOTIATION_DISABLED" in str(ssl_err):
  6. ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
  7. ctx.options |= 0x4
  8. self._sess.mount("https://", CustomHttpAdapter(ctx))
  9. return req()
  10. raise

The issue I'm having is testing it. I've tried doing this:

  1. err = SSLError()
  2. 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:

  1. try:
  2. return req()
  3. except (ssl.SSLError, RSSLError) as ssl_err:
  4. print(ssl_err)
  5. if "UNSAFE_LEGACY_RENEGOTIATION_DISABLED" in str(ssl_err):
  6. ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
  7. ctx.options |= 0x4
  8. self._sess.mount("https://", CustomHttpAdapter(ctx))
  9. return req()
  10. raise

The issue I'm having is testing it. I've tried doing this:

  1. err = SSLError()
  2. 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

你需要将该字符串作为构造函数的参数传递:

  1. >>> from ssl import SSLError
  2. >>> error = SSLError("UNSAFE_LEGACY_RENEGOTIATION_DISABLED")
  3. >>> print(error)
  4. ('UNSAFE_LEGACY_RENEGOTIATION_DISABLED',)
英文:

You need to pass the string as your argument to the constructor:

  1. >>> from ssl import SSLError
  2. >>> error = SSLError("UNSAFE_LEGACY_RENEGOTIATION_DISABLED")
  3. >>> print(error)
  4. ('UNSAFE_LEGACY_RENEGOTIATION_DISABLED',)

huangapple
  • 本文由 发表于 2023年7月28日 01:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定