MCPcopy Index your code
hub / github.com/python/cpython / _ipaddr_info

Function _ipaddr_info

Lib/asyncio/base_events.py:102–158  ·  view source on GitHub ↗
(host, port, family, type, proto, flowinfo=0, scopeid=0)

Source from the content-addressed store, hash-verified

100
101
102def _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

Callers 1

_ensure_resolvedMethod · 0.85

Calls 2

appendMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…