Python socket.NI_NUMERICHOST Examples

The following are 19 code examples of socket.NI_NUMERICHOST(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module socket , or try the search function .
Example #1
Source File: tcp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _getrealname(addr):
    """
    Return a 2-tuple of socket IP and port for IPv4 and a 4-tuple of
    socket IP, port, flowInfo, and scopeID for IPv6.  For IPv6, it
    returns the interface portion (the part after the %) as a part of
    the IPv6 address, which Python 3.7+ does not include.

    @param addr: A 2-tuple for IPv4 information or a 4-tuple for IPv6
        information.
    """
    if len(addr) == 4:
        # IPv6
        host = socket.getnameinfo(
            addr, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV)[0]
        return tuple([host] + list(addr[1:]))
    else:
        return addr[:2] 
Example #2
Source File: tcp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _setRealAddress(self, address):
        """
        Set the resolved address of this L{_BaseBaseClient} and initiate the
        connection attempt.

        @param address: Depending on whether this is an IPv4 or IPv6 connection
            attempt, a 2-tuple of C{(host, port)} or a 4-tuple of C{(host,
            port, flow, scope)}.  At this point it is a fully resolved address,
            and the 'host' portion will always be an IP address, not a DNS
            name.
        """
        if len(address) == 4:
            # IPv6, make sure we have the scopeID associated
            hostname = socket.getnameinfo(
                address, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV)[0]
            self.realAddress = tuple([hostname] + list(address[1:]))
        else:
            self.realAddress = address
        self.doConnect() 
Example #3
Source File: test_tcp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_serverGetHostOnIPv6ScopeID(self):
        """
        When a connection is accepted over IPv6, the server
        L{ITransport.getHost} method returns an L{IPv6Address} giving the
        address on which the server accepted the connection, including the scope
        identifier.
        """
        interface = getLinkLocalIPv6Address()
        client = createTestSocket(self, socket.AF_INET6, socket.SOCK_STREAM)
        hostAddress = self._serverGetConnectionAddressTest(
            client, interface, 'getHost')

        peer = client.getpeername()
        hostname = socket.getnameinfo(peer, socket.NI_NUMERICHOST)[0]

        self.assertEqual(
            IPv6Address('TCP', hostname, *peer[1:]), hostAddress) 
Example #4
Source File: test_tcp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_serverGetPeerOnIPv6(self):
        """
        When a connection is accepted over IPv6, the server
        L{ITransport.getPeer} method returns an L{IPv6Address} giving the
        address on the remote end of the connection.
        """
        interface = '::1'
        client = createTestSocket(self, socket.AF_INET6, socket.SOCK_STREAM)
        peerAddress = self._serverGetConnectionAddressTest(
            client, interface, 'getPeer')

        peer = client.getsockname()
        hostname = socket.getnameinfo(peer, socket.NI_NUMERICHOST)[0]

        self.assertEqual(
            IPv6Address('TCP', hostname, *peer[1:]), peerAddress) 
Example #5
Source File: test_tcp.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_serverGetPeerOnIPv6ScopeID(self):
        """
        When a connection is accepted over IPv6, the server
        L{ITransport.getPeer} method returns an L{IPv6Address} giving the
        address on the remote end of the connection, including the scope
        identifier.
        """
        interface = getLinkLocalIPv6Address()
        client = createTestSocket(self, socket.AF_INET6, socket.SOCK_STREAM)
        peerAddress = self._serverGetConnectionAddressTest(
            client, interface, 'getPeer')

        peer = client.getsockname()
        hostname = socket.getnameinfo(peer, socket.NI_NUMERICHOST)[0]

        self.assertEqual(
            IPv6Address('TCP', hostname, *peer[1:]), peerAddress) 
Example #6
Source File: test_socket.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def testPort(self):
        for address, flags, expected in [
            ( ("127.0.0.1", 25),  0,                     "smtp" ),
            ( ("127.0.0.1", 25),  socket.NI_NUMERICSERV, 25     ),

            # This portion of the test does not suceed on OS X;
            # the above entries probably suffice
            # ( ("127.0.0.1", 513), socket.NI_DGRAM,       "who"  ),
            # ( ("127.0.0.1", 513), 0,                     "login"),
        ]:
            result = socket.getnameinfo(address, flags)
            self.failUnlessEqual(result[1], expected)


    # This test currently fails due to the recent changes (as of March 2014) at python.org:
    # TBD perhaps there are well-known addresses that guarantee stable resolution

    # def testHost(self):
    #     for address, flags, expected in [
    #         ( ("www.python.org", 80),  0,                     "dinsdale.python.org"),
    #         ( ("www.python.org", 80),  socket.NI_NUMERICHOST, "82.94.164.162"      ),
    #         ( ("www.python.org", 80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #         ( ("82.94.164.162",  80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #     ]:
    #         result = socket.getnameinfo(address, flags)
    #         self.failUnlessEqual(result[0], expected) 
Example #7
Source File: udp.py    From nfcpy with European Union Public License 1.1 5 votes vote down vote up
def __init__(self, host, port):
        host = socket.gethostbyname(host)
        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() 
Example #8
Source File: resolver.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #9
Source File: resolver.py    From arissploit with GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #10
Source File: resolver.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #11
Source File: test_socket.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def testPort(self):
        for address, flags, expected in [
            ( ("127.0.0.1", 25),  0,                     "smtp" ),
            ( ("127.0.0.1", 25),  socket.NI_NUMERICSERV, 25     ),

            # This portion of the test does not suceed on OS X;
            # the above entries probably suffice
            # ( ("127.0.0.1", 513), socket.NI_DGRAM,       "who"  ),
            # ( ("127.0.0.1", 513), 0,                     "login"),
        ]:
            result = socket.getnameinfo(address, flags)
            self.failUnlessEqual(result[1], expected)


    # This test currently fails due to the recent changes (as of March 2014) at python.org:
    # TBD perhaps there are well-known addresses that guarantee stable resolution

    # def testHost(self):
    #     for address, flags, expected in [
    #         ( ("www.python.org", 80),  0,                     "dinsdale.python.org"),
    #         ( ("www.python.org", 80),  socket.NI_NUMERICHOST, "82.94.164.162"      ),
    #         ( ("www.python.org", 80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #         ( ("82.94.164.162",  80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
    #     ]:
    #         result = socket.getnameinfo(address, flags)
    #         self.failUnlessEqual(result[0], expected) 
Example #12
Source File: resolver.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #13
Source File: resolver.py    From Cloudmare with GNU General Public License v3.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = thirdparty.dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (thirdparty.dns.resolver.NXDOMAIN, thirdparty.dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #14
Source File: test_socket.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def testHost(self):
        for address, flags, expected in [
            ( ("www.python.org", 80),  0,                     "dinsdale.python.org"),
            ( ("www.python.org", 80),  socket.NI_NUMERICHOST, "82.94.164.162"      ),
            ( ("www.python.org", 80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
            ( ("82.94.164.162",  80),  socket.NI_NAMEREQD,    "dinsdale.python.org"),
        ]:
            result = socket.getnameinfo(address, flags)
            self.failUnlessEqual(result[0], expected) 
Example #15
Source File: resolver.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #16
Source File: resolver.py    From script.elementum.burst with Do What The F*ck You Want To Public License 5 votes vote down vote up
def _getnameinfo(sockaddr, flags=0):
    host = sockaddr[0]
    port = sockaddr[1]
    if len(sockaddr) == 4:
        scope = sockaddr[3]
        family = socket.AF_INET6
    else:
        scope = None
        family = socket.AF_INET
    tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM,
                          socket.SOL_TCP, 0)
    if len(tuples) > 1:
        raise socket.error('sockaddr resolved to multiple addresses')
    addr = tuples[0][4][0]
    if flags & socket.NI_DGRAM:
        pname = 'udp'
    else:
        pname = 'tcp'
    qname = dns.reversename.from_address(addr)
    if flags & socket.NI_NUMERICHOST == 0:
        try:
            answer = _resolver.query(qname, 'PTR')
            hostname = answer.rrset[0].target.to_text(True)
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
            if flags & socket.NI_NAMEREQD:
                raise socket.gaierror(socket.EAI_NONAME)
            hostname = addr
            if scope is not None:
                hostname += '%' + str(scope)
    else:
        hostname = addr
        if scope is not None:
            hostname += '%' + str(scope)
    if flags & socket.NI_NUMERICSERV:
        service = str(port)
    else:
        service = socket.getservbyport(port, pname)
    return (hostname, service) 
Example #17
Source File: test_tcp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_buildProtocolIPv6AddressScopeID(self):
        """
        When a connection is accepted to a link-local IPv6 address, an
        L{IPv6Address} is passed to the factory's C{buildProtocol} method
        giving the peer's address, including a scope identifier.
        """
        interface = getLinkLocalIPv6Address()
        client = createTestSocket(self, socket.AF_INET6, socket.SOCK_STREAM)
        observedAddress = self._buildProtocolAddressTest(client, interface)

        peer = client.getsockname()
        hostname = socket.getnameinfo(peer, socket.NI_NUMERICHOST)[0]

        self.assertEqual(
            IPv6Address('TCP', hostname, *peer[1:]), observedAddress) 
Example #18
Source File: test_tcp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_buildProtocolIPv6Address(self):
        """
        When a connection is accepted to an IPv6 address, an L{IPv6Address} is
        passed to the factory's C{buildProtocol} method giving the peer's
        address.
        """
        interface = '::1'
        client = createTestSocket(self, socket.AF_INET6, socket.SOCK_STREAM)
        observedAddress = self._buildProtocolAddressTest(client, interface)

        peer = client.getsockname()
        hostname = socket.getnameinfo(peer, socket.NI_NUMERICHOST)[0]

        self.assertEqual(
            IPv6Address('TCP', hostname, peer[1]), observedAddress) 
Example #19
Source File: connectionmixins.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def findFreePort(interface='127.0.0.1', family=socket.AF_INET,
                 type=socket.SOCK_STREAM):
    """
    Ask the platform to allocate a free port on the specified interface, then
    release the socket and return the address which was allocated.

    @param interface: The local address to try to bind the port on.
    @type interface: C{str}

    @param type: The socket type which will use the resulting port.

    @return: A two-tuple of address and port, like that returned by
        L{socket.getsockname}.
    """
    addr = socket.getaddrinfo(interface, 0)[0][4]
    probe = socket.socket(family, type)
    try:
        probe.bind(addr)
        if family == socket.AF_INET6:
            sockname = probe.getsockname()
            hostname = socket.getnameinfo(
                sockname, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV)[0]
            return (hostname, sockname[1])
        else:
            return probe.getsockname()
    finally:
        probe.close()