Create, bind and connect one socket.
(self, exceptions, addr_info, local_addr_infos=None)
| 1010 | offset)) |
| 1011 | |
| 1012 | async def _connect_sock(self, exceptions, addr_info, local_addr_infos=None): |
| 1013 | """Create, bind and connect one socket.""" |
| 1014 | my_exceptions = [] |
| 1015 | exceptions.append(my_exceptions) |
| 1016 | family, type_, proto, _, address = addr_info |
| 1017 | sock = None |
| 1018 | try: |
| 1019 | try: |
| 1020 | sock = socket.socket(family=family, type=type_, proto=proto) |
| 1021 | sock.setblocking(False) |
| 1022 | if local_addr_infos is not None: |
| 1023 | for lfamily, _, _, _, laddr in local_addr_infos: |
| 1024 | # skip local addresses of different family |
| 1025 | if lfamily != family: |
| 1026 | continue |
| 1027 | try: |
| 1028 | sock.bind(laddr) |
| 1029 | break |
| 1030 | except OSError as exc: |
| 1031 | msg = ( |
| 1032 | f'error while attempting to bind on ' |
| 1033 | f'address {laddr!r}: {str(exc).lower()}' |
| 1034 | ) |
| 1035 | exc = OSError(exc.errno, msg) |
| 1036 | my_exceptions.append(exc) |
| 1037 | else: # all bind attempts failed |
| 1038 | if my_exceptions: |
| 1039 | raise my_exceptions.pop() |
| 1040 | else: |
| 1041 | raise OSError(f"no matching local address with {family=} found") |
| 1042 | await self.sock_connect(sock, address) |
| 1043 | return sock |
| 1044 | except OSError as exc: |
| 1045 | my_exceptions.append(exc) |
| 1046 | raise |
| 1047 | except: |
| 1048 | if sock is not None: |
| 1049 | try: |
| 1050 | sock.close() |
| 1051 | except OSError: |
| 1052 | # An error when closing a newly created socket is |
| 1053 | # not important, but it can overwrite more important |
| 1054 | # non-OSError error. So ignore it. |
| 1055 | pass |
| 1056 | raise |
| 1057 | finally: |
| 1058 | exceptions = my_exceptions = None |
| 1059 | |
| 1060 | async def create_connection( |
| 1061 | self, protocol_factory, host=None, port=None, |
no test coverage detected