(host, port, family, type, proto, flowinfo=0, scopeid=0)
| 100 | |
| 101 | |
| 102 | def _ipaddr_info(host, port, family, type, proto, flowinfo=0, scopeid=0): |
| 103 | # Try to skip getaddrinfo if "host" is already an IP. Users might have |
| 104 | # handled name resolution in their own code and pass in resolved IPs. |
| 105 | if not hasattr(socket, 'inet_pton'): |
| 106 | return |
| 107 | |
| 108 | if proto not in {0, socket.IPPROTO_TCP, socket.IPPROTO_UDP} or \ |
| 109 | host is None: |
| 110 | return None |
| 111 | |
| 112 | if type == socket.SOCK_STREAM: |
| 113 | proto = socket.IPPROTO_TCP |
| 114 | elif type == socket.SOCK_DGRAM: |
| 115 | proto = socket.IPPROTO_UDP |
| 116 | else: |
| 117 | return None |
| 118 | |
| 119 | if port is None: |
| 120 | port = 0 |
| 121 | elif isinstance(port, bytes) and port == b'': |
| 122 | port = 0 |
| 123 | elif isinstance(port, str) and port == '': |
| 124 | port = 0 |
| 125 | else: |
| 126 | # If port's a service name like "http", don't skip getaddrinfo. |
| 127 | try: |
| 128 | port = int(port) |
| 129 | except (TypeError, ValueError): |
| 130 | return None |
| 131 | |
| 132 | if family == socket.AF_UNSPEC: |
| 133 | afs = [socket.AF_INET] |
| 134 | if _HAS_IPv6: |
| 135 | afs.append(socket.AF_INET6) |
| 136 | else: |
| 137 | afs = [family] |
| 138 | |
| 139 | if isinstance(host, bytes): |
| 140 | host = host.decode('idna') |
| 141 | if '%' in host: |
| 142 | # Linux's inet_pton doesn't accept an IPv6 zone index after host, |
| 143 | # like '::1%lo0'. |
| 144 | return None |
| 145 | |
| 146 | for af in afs: |
| 147 | try: |
| 148 | socket.inet_pton(af, host) |
| 149 | # The host has already been resolved. |
| 150 | if _HAS_IPv6 and af == socket.AF_INET6: |
| 151 | return af, type, proto, '', (host, port, flowinfo, scopeid) |
| 152 | else: |
| 153 | return af, type, proto, '', (host, port) |
| 154 | except OSError: |
| 155 | pass |
| 156 | |
| 157 | # "host" is not an IP address. |
| 158 | return None |
| 159 |
no test coverage detected
searching dependent graphs…