Returns an ``ssl.SSLSocket`` wrapping the given socket. ``ssl_options`` may be either an `ssl.SSLContext` object or a dictionary (as accepted by `ssl_options_to_context`). Additional keyword arguments are passed to ``wrap_socket`` (either the `~ssl.SSLContext` method or the `ssl` m
(
socket: socket.socket,
ssl_options: Union[Dict[str, Any], ssl.SSLContext],
server_hostname: Optional[str] = None,
server_side: Optional[bool] = None,
**kwargs: Any
)
| 642 | |
| 643 | |
| 644 | def ssl_wrap_socket( |
| 645 | socket: socket.socket, |
| 646 | ssl_options: Union[Dict[str, Any], ssl.SSLContext], |
| 647 | server_hostname: Optional[str] = None, |
| 648 | server_side: Optional[bool] = None, |
| 649 | **kwargs: Any |
| 650 | ) -> ssl.SSLSocket: |
| 651 | """Returns an ``ssl.SSLSocket`` wrapping the given socket. |
| 652 | |
| 653 | ``ssl_options`` may be either an `ssl.SSLContext` object or a |
| 654 | dictionary (as accepted by `ssl_options_to_context`). Additional |
| 655 | keyword arguments are passed to ``wrap_socket`` (either the |
| 656 | `~ssl.SSLContext` method or the `ssl` module function as |
| 657 | appropriate). |
| 658 | |
| 659 | .. versionchanged:: 6.2 |
| 660 | |
| 661 | Added server_side argument. Omitting this argument will |
| 662 | result in a DeprecationWarning on Python 3.10. |
| 663 | """ |
| 664 | context = ssl_options_to_context(ssl_options, server_side=server_side) |
| 665 | if server_side is None: |
| 666 | server_side = False |
| 667 | if ssl.HAS_SNI: |
| 668 | # In python 3.4, wrap_socket only accepts the server_hostname |
| 669 | # argument if HAS_SNI is true. |
| 670 | # TODO: add a unittest (python added server-side SNI support in 3.4) |
| 671 | # In the meantime it can be manually tested with |
| 672 | # python3 -m tornado.httpclient https://sni.velox.ch |
| 673 | return context.wrap_socket( |
| 674 | socket, server_hostname=server_hostname, server_side=server_side, **kwargs |
| 675 | ) |
| 676 | else: |
| 677 | return context.wrap_socket(socket, server_side=server_side, **kwargs) |