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)
| 422 | |
| 423 | |
| 424 | def _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 | |
| 473 | def _configured_socket_interface(address: _Address, options: PoolOptions) -> NetworkingInterface: |
no test coverage detected