我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用socket.getnameinfo()。
def override_system_resolver(resolver=None): """Override the system resolver routines in the socket module with versions which use dnspython's resolver. This can be useful in testing situations where you want to control the resolution behavior of python code without having to change the system's resolver settings (e.g. /etc/resolv.conf). The resolver to use may be specified; if it's not, the default resolver will be used. @param resolver: the resolver to use @type resolver: dns.resolver.Resolver object or None """ if resolver is None: resolver = get_default_resolver() global _resolver _resolver = resolver socket.getaddrinfo = _getaddrinfo socket.getnameinfo = _getnameinfo socket.getfqdn = _getfqdn socket.gethostbyname = _gethostbyname socket.gethostbyname_ex = _gethostbyname_ex socket.gethostbyaddr = _gethostbyaddr
def getnameinfo(sockaddr, flags): """Look up a name given a numeric address. Arguments and return values are identical to :func:`socket.getnameinfo`, except that this version is async. This function's behavior can be customized using :func:`set_custom_hostname_resolver`. """ hr = _overrides.hostname_resolver if hr is not None: return await hr.getnameinfo(sockaddr, flags) else: return await run_sync_in_worker_thread( _stdlib_socket.getnameinfo, sockaddr, flags, cancellable=True )
def override_system_resolver(resolver=None): """Override the system resolver routines in the socket module with versions which use dnspython's resolver. This can be useful in testing situations where you want to control the resolution behavior of python code without having to change the system's resolver settings (e.g. /etc/resolv.conf). The resolver to use may be specified; if it's not, the default resolver will be used. @param resolver: the resolver to use @type resolver: resolver.Resolver object or None """ if resolver is None: resolver = get_default_resolver() global _resolver _resolver = resolver socket.getaddrinfo = _getaddrinfo socket.getnameinfo = _getnameinfo socket.getfqdn = _getfqdn socket.gethostbyname = _gethostbyname socket.gethostbyname_ex = _gethostbyname_ex socket.gethostbyaddr = _gethostbyaddr
def restore_system_resolver(): """Undo the effects of override_system_resolver(). """ global _resolver _resolver = None socket.getaddrinfo = _original_getaddrinfo socket.getnameinfo = _original_getnameinfo socket.getfqdn = _original_getfqdn socket.gethostbyname = _original_gethostbyname socket.gethostbyname_ex = _original_gethostbyname_ex socket.gethostbyaddr = _original_gethostbyaddr
def test_getnameinfo(self): sockaddr = mock.Mock() self.loop.run_in_executor = mock.Mock() self.loop.getnameinfo(sockaddr) self.assertEqual( (None, socket.getnameinfo, sockaddr, 0), self.loop.run_in_executor.call_args[0])
def __init__(self, host, port): host, port = socket.getnameinfo((host, port), socket.NI_NUMERICHOST) self.addr = (host, int(port)) self._path = "%s:%s" % (host, port) self.socket = None self._create_socket()
def set_custom_hostname_resolver(hostname_resolver): """Set a custom hostname resolver. By default, trio's :func:`getaddrinfo` and :func:`getnameinfo` functions use the standard system resolver functions. This function allows you to customize that behavior. The main intended use case is for testing, but it might also be useful for using third-party resolvers like `c-ares <https://c-ares.haxx.se/>`__ (though be warned that these rarely make perfect drop-in replacements for the system resolver). See :class:`trio.abc.HostnameResolver` for more details. Setting a custom hostname resolver affects all future calls to :func:`getaddrinfo` and :func:`getnameinfo` within the enclosing call to :func:`trio.run`. All other hostname resolution in trio is implemented in terms of these functions. Generally you should call this function just once, right at the beginning of your program. Args: hostname_resolver (trio.abc.HostnameResolver or None): The new custom hostname resolver, or None to restore the default behavior. Returns: The previous hostname resolver (which may be None). """ old = _overrides.hostname_resolver _overrides.hostname_resolver = hostname_resolver return old
def test_getnameinfo(): # Trivial test: ni_numeric = stdlib_socket.NI_NUMERICHOST | stdlib_socket.NI_NUMERICSERV with assert_checkpoints(): got = await tsocket.getnameinfo(("127.0.0.1", 1234), ni_numeric) assert got == ("127.0.0.1", "1234") # getnameinfo requires a numeric address as input: with assert_checkpoints(): with pytest.raises(tsocket.gaierror): await tsocket.getnameinfo(("google.com", 80), 0) with assert_checkpoints(): with pytest.raises(tsocket.gaierror): await tsocket.getnameinfo(("localhost", 80), 0) # Blocking call to get expected values: host, service = stdlib_socket.getnameinfo(("127.0.0.1", 80), 0) # Some working calls: got = await tsocket.getnameinfo(("127.0.0.1", 80), 0) assert got == (host, service) got = await tsocket.getnameinfo(("127.0.0.1", 80), tsocket.NI_NUMERICHOST) assert got == ("127.0.0.1", service) got = await tsocket.getnameinfo(("127.0.0.1", 80), tsocket.NI_NUMERICSERV) assert got == (host, "80") ################################################################ # constructors ################################################################
def test_custom_hostname_resolver(monkeygai): class CustomResolver: async def getaddrinfo(self, host, port, family, type, proto, flags): return ("custom_gai", host, port, family, type, proto, flags) async def getnameinfo(self, sockaddr, flags): return ("custom_gni", sockaddr, flags) cr = CustomResolver() assert tsocket.set_custom_hostname_resolver(cr) is None # Check that the arguments are all getting passed through. # We have to use valid calls to avoid making the underlying system # getaddrinfo cranky when it's used for NUMERIC checks. for vals in [ (tsocket.AF_INET, 0, 0, 0), (0, tsocket.SOCK_STREAM, 0, 0), (0, 0, tsocket.IPPROTO_TCP, 0), (0, 0, 0, tsocket.AI_CANONNAME), ]: assert ( await tsocket.getaddrinfo( "localhost", "foo", *vals ) == ("custom_gai", b"localhost", "foo", *vals) ) # IDNA encoding is handled before calling the special object got = await tsocket.getaddrinfo("föö", "foo") expected = ("custom_gai", b"xn--f-1gaa", "foo", 0, 0, 0, 0) assert got == expected assert (await tsocket.getnameinfo("a", 0) == ("custom_gni", "a", 0)) # We can set it back to None assert tsocket.set_custom_hostname_resolver(None) is cr # And now trio switches back to calling socket.getaddrinfo (specifically # our monkeypatched version of socket.getaddrinfo) monkeygai.set("x", b"host", "port", family=0, type=0, proto=0, flags=0) assert await tsocket.getaddrinfo("host", "port") == "x"
def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo if hasattr(sys, "getrefcount"): try: # On some versions, this loses a reference orig = sys.getrefcount(__name__) socket.getnameinfo(__name__,0) except TypeError: if sys.getrefcount(__name__) != orig: self.fail("socket.getnameinfo loses a reference")
def testInterpreterCrash(self): # Making sure getnameinfo doesn't crash the interpreter try: # On some versions, this crashes the interpreter. socket.getnameinfo(('x', 0, 0, 0), 0) except socket.error: pass
def test_getnameinfo(self): # only IP addresses are allowed self.assertRaises(socket.error, socket.getnameinfo, ('mail.python.org',0), 0)
def test_flowinfo(self): self.assertRaises(OverflowError, socket.getnameinfo, ('::1',0, 0xffffffff), 0) with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: self.assertRaises(OverflowError, s.bind, ('::1', 0, -10))
def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo try: # On some versions, this loses a reference orig = sys.getrefcount(__name__) socket.getnameinfo(__name__,0) except TypeError: self.assertEqual(sys.getrefcount(__name__), orig, "socket.getnameinfo loses a reference")
def test_flowinfo(self): self.assertRaises(OverflowError, socket.getnameinfo, ('::1',0, 0xffffffff), 0) s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) try: self.assertRaises(OverflowError, s.bind, ('::1', 0, -10)) finally: s.close()
def getnameinfo(self, sockaddr, flags=0): return self.run_in_executor(None, socket.getnameinfo, sockaddr, flags)
def testRefCountGetNameInfo(self): # Testing reference count for getnameinfo try: # On some versions, this loses a reference orig = sys.getrefcount(__name__) socket.getnameinfo(__name__,0) except TypeError: if sys.getrefcount(__name__) != orig: self.fail("socket.getnameinfo loses a reference")
def testInterpreterCrash(self): # Making sure getnameinfo doesn't crash the interpreter try: # On some versions, this crashes the interpreter. socket.getnameinfo(('x', 0, 0, 0), 0) except OSError: pass