MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _configured_socket

Function _configured_socket

pymongo/pool_shared.py:424–470  ·  view source on GitHub ↗

Given (host, port) and PoolOptions, return a raw configured socket. Can raise socket.error, ConnectionFailure, or _CertificateError. Sets socket's SSL and timeout options.

(address: _Address, options: PoolOptions)

Source from the content-addressed store, hash-verified

422
423
424def _configured_socket(address: _Address, options: PoolOptions) -> Union[socket.socket, _sslConn]:
425 """Given (host, port) and PoolOptions, return a raw configured socket.
426
427 Can raise socket.error, ConnectionFailure, or _CertificateError.
428
429 Sets socket's SSL and timeout options.
430 """
431 sock = _create_connection(address, options)
432 ssl_context = options._ssl_context
433
434 if ssl_context is None:
435 sock.settimeout(options.socket_timeout)
436 return sock
437
438 host = address[0]
439 try:
440 # We have to pass hostname / ip address to wrap_socket
441 # to use SSLContext.check_hostname.
442 if _has_sni(True):
443 ssl_sock = ssl_context.wrap_socket(sock, server_hostname=host) # type: ignore[assignment, misc, unused-ignore]
444 else:
445 ssl_sock = ssl_context.wrap_socket(sock) # type: ignore[assignment, misc, unused-ignore]
446 except _CertificateError:
447 sock.close()
448 # Raise _CertificateError directly like we do after match_hostname
449 # below.
450 raise
451 except (OSError, *SSLErrors) as exc:
452 sock.close()
453 # We raise AutoReconnect for transient and permanent SSL handshake
454 # failures alike. Permanent handshake failures, like protocol
455 # mismatch, will be turned into ServerSelectionTimeoutErrors later.
456 details = _get_timeout_details(options)
457 _raise_connection_failure(address, exc, "SSL handshake failed: ", timeout_details=details)
458 if (
459 ssl_context.verify_mode
460 and not ssl_context.check_hostname
461 and not options.tls_allow_invalid_hostnames
462 ):
463 try:
464 ssl.match_hostname(ssl_sock.getpeercert(), hostname=host) # type:ignore[attr-defined, unused-ignore]
465 except _CertificateError:
466 ssl_sock.close()
467 raise
468
469 ssl_sock.settimeout(options.socket_timeout)
470 return ssl_sock
471
472
473def _configured_socket_interface(address: _Address, options: PoolOptions) -> NetworkingInterface:

Callers 1

_connect_kmsFunction · 0.90

Calls 7

_has_sniFunction · 0.90
_get_timeout_detailsFunction · 0.90
_create_connectionFunction · 0.85
wrap_socketMethod · 0.80
settimeoutMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected