MCPcopy Create free account
hub / github.com/PyMySQL/PyMySQL / _create_ssl_ctx

Method _create_ssl_ctx

pymysql/connections.py:387–425  ·  view source on GitHub ↗
(self, sslp)

Source from the content-addressed store, hash-verified

385 self.close()
386
387 def _create_ssl_ctx(self, sslp):
388 if isinstance(sslp, ssl.SSLContext):
389 return sslp
390 ca = sslp.get("ca")
391 capath = sslp.get("capath")
392 hasnoca = ca is None and capath is None
393 ctx = ssl.create_default_context(cafile=ca, capath=capath)
394
395 # Python 3.13 enables VERIFY_X509_STRICT by default.
396 # But self signed certificates that are generated by MySQL automatically
397 # doesn't pass the verification.
398 ctx.verify_flags &= ~ssl.VERIFY_X509_STRICT
399
400 ctx.check_hostname = not hasnoca and sslp.get("check_hostname", True)
401 verify_mode_value = sslp.get("verify_mode")
402 if verify_mode_value is None:
403 ctx.verify_mode = ssl.CERT_NONE if hasnoca else ssl.CERT_REQUIRED
404 elif isinstance(verify_mode_value, bool):
405 ctx.verify_mode = ssl.CERT_REQUIRED if verify_mode_value else ssl.CERT_NONE
406 else:
407 if isinstance(verify_mode_value, str):
408 verify_mode_value = verify_mode_value.lower()
409 if verify_mode_value in ("none", "0", "false", "no"):
410 ctx.verify_mode = ssl.CERT_NONE
411 elif verify_mode_value == "optional":
412 ctx.verify_mode = ssl.CERT_OPTIONAL
413 elif verify_mode_value in ("required", "1", "true", "yes"):
414 ctx.verify_mode = ssl.CERT_REQUIRED
415 else:
416 ctx.verify_mode = ssl.CERT_NONE if hasnoca else ssl.CERT_REQUIRED
417 if "cert" in sslp:
418 ctx.load_cert_chain(
419 sslp["cert"], keyfile=sslp.get("key"), password=sslp.get("password")
420 )
421 if "cipher" in sslp:
422 ctx.set_ciphers(sslp["cipher"])
423 ctx.options |= ssl.OP_NO_SSLv2
424 ctx.options |= ssl.OP_NO_SSLv3
425 return ctx
426
427 def close(self):
428 """

Callers 1

__init__Method · 0.95

Calls 1

getMethod · 0.80

Tested by

no test coverage detected