我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用socket.AI_CANONNAME。
def _gethostbyaddr(ip): try: dns.ipv6.inet_aton(ip) sockaddr = (ip, 80, 0, 0) family = socket.AF_INET6 except: sockaddr = (ip, 80) family = socket.AF_INET (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD) aliases = [] addresses = [] tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME) canonical = tuples[0][3] for item in tuples: addresses.append(item[4][0]) # XXX we just ignore aliases return (canonical, aliases, addresses)
def _gethostbyaddr(ip): try: addr = dns.ipv6.inet_aton(ip) sockaddr = (ip, 80, 0, 0) family = socket.AF_INET6 except: sockaddr = (ip, 80) family = socket.AF_INET (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD) aliases = [] addresses = [] tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME) canonical = tuples[0][3] for item in tuples: addresses.append(item[4][0]) # XXX we just ignore aliases return (canonical, aliases, addresses)
def get_fqdn(): """ Return the current fqdn of the machine, trying hard to return a meaningful name. In particular, it means working against a NetworkManager bug which seems to make C{getfqdn} return localhost6.localdomain6 for machine without a domain since Maverick. """ fqdn = socket.getfqdn() if "localhost" in fqdn: # Try the heavy artillery fqdn = socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME)[0][3] if "localhost" in fqdn: # Another fallback fqdn = socket.gethostname() return fqdn
def _gethostbyaddr(ip): try: dns.ipv6.inet_aton(ip) sockaddr = (ip, 80, 0, 0) family = socket.AF_INET6 except Exception: sockaddr = (ip, 80) family = socket.AF_INET (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD) aliases = [] addresses = [] tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME) canonical = tuples[0][3] for item in tuples: addresses.append(item[4][0]) # XXX we just ignore aliases return (canonical, aliases, addresses)
def hostname(): """Hostname of the server.""" host_name = socket.gethostname() port = 0 family = 0 socktype = 0 proto = 0 _family, _socktype, _proto, canonname, _sockaddr = socket.getaddrinfo( host_name, port, family, socktype, proto, socket.AI_CANONNAME)[0] return canonname.lower()
def _Dynamic_Resolve(self, request, response): addrs = {} for family, _, _, canonname, sa in socket.getaddrinfo( request.name(), 0, 0, socket.SOCK_STREAM, 0, socket.AI_CANONNAME): addrs.setdefault(self._AF_MAP.get(family), set()).add( _remote_socket_addr.inet_pton(self._TRANSLATED_AF_MAP[family], sa[0])) response.set_canonical_name(canonname) if canonname and canonname.lower() != request.name().lower(): if not response.aliases_size(): response.add_aliases(request.name()) for af in request.address_families_list(): for packed_addr in addrs.get(af, set()): response.add_packed_address(packed_addr)
def _gethostbyaddr(ip): try: addr = ipv6.inet_aton(ip) sockaddr = (ip, 80, 0, 0) family = socket.AF_INET6 except: sockaddr = (ip, 80) family = socket.AF_INET (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD) aliases = [] addresses = [] tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME) canonical = tuples[0][3] for item in tuples: addresses.append(item[4][0]) # XXX we just ignore aliases return (canonical, aliases, addresses)
def _gethostbyname_ex(name): aliases = [] addresses = [] tuples = _getaddrinfo(name, 0, socket.AF_INET, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME) canonical = tuples[0][3] for item in tuples: addresses.append(item[4][0]) # XXX we just ignore aliases return (canonical, aliases, addresses)
def check_ipaddr(dst, version=4): ''' Receive a hostname or IP address string and return a validated IP and fqdn if either can be found ''' family = version_to_family(version) for res in socket.getaddrinfo(dst, 0, family, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_CANONNAME): family, socktype, proto, canonname, sa = res try: addr = ipaddress.ip_address(unicode(sa[0])) except NameError: addr = ipaddress.ip_address(sa[0]) return addr, canonname
def hostname(self): return socket.getaddrinfo(socket.gethostname(), 0, 0, 0, 0, socket.AI_CANONNAME)[0][3]
def test_canonname(self): greendns.resolve = _make_mock_resolve() greendns.resolve.add('host.example.com', '1.2.3.4') greendns.resolve_cname = self._make_mock_resolve_cname() res = greendns.getaddrinfo('host.example.com', 0, 0, 0, 0, socket.AI_CANONNAME) assert res[0][3] == 'cname.example.com'
def __str__(self): if self.fqdn is None: # # If the SSH config contains AddressFamily, use that when # determining the local host's FQDN. Using socket.getfqdn() from # the standard library is the most general solution, but can # result in noticeable delays on some platforms when IPv6 is # misconfigured or not available, as it calls getaddrinfo with no # address family specified, so both IPv4 and IPv6 are checked. # # Handle specific option fqdn = None address_family = self.config.get('addressfamily', 'any').lower() if address_family != 'any': try: family = socket.AF_INET if address_family == 'inet' \ else socket.AF_INET6 results = socket.getaddrinfo( self.host, None, family, socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME ) for res in results: af, socktype, proto, canonname, sa = res if canonname and '.' in canonname: fqdn = canonname break # giaerror -> socket.getaddrinfo() can't resolve self.host # (which is from socket.gethostname()). Fall back to the # getfqdn() call below. except socket.gaierror: pass # Handle 'any' / unspecified if fqdn is None: fqdn = socket.getfqdn() # Cache self.fqdn = fqdn return self.fqdn
def setup(procname, debug = False): # XXX chicken egg runinfo['service'] = procname runinfo['host'] = socket.getaddrinfo(socket.gethostname(), 0, 0, 0, 0, socket.AI_CANONNAME)[0][3] runinfo['debug'] = debug logging.config.dictConfig({ "version": 1, "formatters": { "default": { "format": " ".join([ "%s[%%(process)s]:" % (procname, ), "%(levelname)s:", "%(message)s" ]), "datefmt": "%Y/%m/%d %H:%M:%S", }, "exception": { "format": "".join([ "-" * 72 + "\n", "%(asctime)s", " %s[%%(process)s]:" % (procname, ), " %(levelname)s:", " %(message)s", ]), "datefmt": "%Y/%m/%d %H:%M:%S", }, }, "handlers": { "syslog": { "class": "logging.handlers.SysLogHandler", "formatter": "default", "level": debug and "DEBUG" or "INFO", "address": "/dev/log", }, "stdout": { "class": "logging.StreamHandler", "formatter": "default", "level": debug and "DEBUG" or "INFO", }, "null": { "class" : "logging.NullHandler", "formatter": "exception", } }, "loggers": { "threadless": { "handlers" : [ debug and "stdout" or "syslog" ], "level": debug and "DEBUG" or "INFO", "propagate": False, }, }, "root": { "handlers": [ debug and "stdout" or "syslog" ], "level": "INFO", } })
def __str__(self): if self.fqdn is None: # # If the SSH config contains AddressFamily, use that when # determining the local host's FQDN. Using socket.getfqdn() from # the standard library is the most general solution, but can # result in noticeable delays on some platforms when IPv6 is # misconfigured or not available, as it calls getaddrinfo with no # address family specified, so both IPv4 and IPv6 are checked. # # Handle specific option fqdn = None address_family = self.config.get('addressfamily', 'any').lower() if address_family != 'any': try: family = socket.AF_INET6 if address_family == 'inet': socket.AF_INET results = socket.getaddrinfo( self.host, None, family, socket.SOCK_DGRAM, socket.IPPROTO_IP, socket.AI_CANONNAME ) for res in results: af, socktype, proto, canonname, sa = res if canonname and '.' in canonname: fqdn = canonname break # giaerror -> socket.getaddrinfo() can't resolve self.host # (which is from socket.gethostname()). Fall back to the # getfqdn() call below. except socket.gaierror: pass # Handle 'any' / unspecified if fqdn is None: fqdn = socket.getfqdn() # Cache self.fqdn = fqdn return self.fqdn