python类gethostbyname()的实例源码

ip.py 文件源码 项目:charm-plumgrid-gateway 作者: openstack 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def get_host_ip(hostname, fallback=None):
    """
    Resolves the IP for a given hostname, or returns
    the input if it is already an IP.
    """
    if is_ip(hostname):
        return hostname

    ip_addr = ns_query(hostname)
    if not ip_addr:
        try:
            ip_addr = socket.gethostbyname(hostname)
        except:
            log("Failed to resolve hostname '%s'" % (hostname),
                level=WARNING)
            return fallback
    return ip_addr
OSC3.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def setOSCTarget(self, address, prefix=None, filters=None):
        """Add (i.e. subscribe) a new OSCTarget, or change the prefix for an existing OSCTarget.
          the 'address' argument can be a ((host, port) tuple) : The target server address & UDP-port
            or a 'host' (string) : The host will be looked-up 
          - prefix (string): The OSC-address prefix prepended to the address of each OSCMessage
          sent to this OSCTarget (optional)
        """
        if type(address) in str:
            address = self._searchHostAddr(address)

        elif (type(address) == tuple):
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except:
                pass

            address = (host, port)
        else:
            raise TypeError("'address' argument must be a (host, port) tuple or a 'host' string")

        self._setTarget(address, prefix, filters)
OSC3.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def delOSCTarget(self, address, prefix=None):
        """Delete the specified OSCTarget from the Client's dict.
        the 'address' argument can be a ((host, port) tuple), or a hostname.
        If the 'prefix' argument is given, the Target is only deleted if the address and prefix match.
        """
        if type(address) in str:
            address = self._searchHostAddr(address) 

        if type(address) == tuple:
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except socket.error:
                pass
            address = (host, port)

            self._delTarget(address, prefix)
OSC3.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def hasOSCTarget(self, address, prefix=None):
        """Return True if the given OSCTarget exists in the Client's dict.
        the 'address' argument can be a ((host, port) tuple), or a hostname.
        If the 'prefix' argument is given, the return-value is only True if the address and prefix match.
        """
        if type(address) in str:
            address = self._searchHostAddr(address) 

        if type(address) == tuple:
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except socket.error:
                pass
            address = (host, port)

            if address in list(self.targets.keys()):
                if prefix == None:
                    return True
                elif prefix == self.targets[address][0]:
                    return True

        return False
OSC3.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 46 收藏 0 点赞 0 评论 0
def getOSCTarget(self, address):
        """Returns the OSCTarget matching the given address as a ((host, port), [prefix, filters]) tuple.
        'address' can be a (host, port) tuple, or a 'host' (string), in which case the first matching OSCTarget is returned
        Returns (None, ['',{}]) if address not found.
        """
        if type(address) in str:
            address = self._searchHostAddr(address) 

        if (type(address) == tuple): 
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except socket.error:
                pass
            address = (host, port)

            if (address in list(self.targets.keys())):
                try:
                    (host, _, _) = socket.gethostbyaddr(host)
                except socket.error:
                    pass

                return ((host, port), self.targets[address])

        return (None, ['',{}])
OSC2.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def setOSCTarget(self, address, prefix=None, filters=None):
        """Add (i.e. subscribe) a new OSCTarget, or change the prefix for an existing OSCTarget.
          the 'address' argument can be a ((host, port) tuple) : The target server address & UDP-port
            or a 'host' (string) : The host will be looked-up 
          - prefix (string): The OSC-address prefix prepended to the address of each OSCMessage
          sent to this OSCTarget (optional)
        """
        if type(address) in types.StringTypes:
            address = self._searchHostAddr(address)

        elif (type(address) == types.TupleType):
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except:
                pass

            address = (host, port)
        else:
            raise TypeError("'address' argument must be a (host, port) tuple or a 'host' string")

        self._setTarget(address, prefix, filters)
OSC2.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def delOSCTarget(self, address, prefix=None):
        """Delete the specified OSCTarget from the Client's dict.
        the 'address' argument can be a ((host, port) tuple), or a hostname.
        If the 'prefix' argument is given, the Target is only deleted if the address and prefix match.
        """
        if type(address) in types.StringTypes:
            address = self._searchHostAddr(address) 

        if type(address) == types.TupleType:
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except socket.error:
                pass
            address = (host, port)

            self._delTarget(address, prefix)
OSC2.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def hasOSCTarget(self, address, prefix=None):
        """Return True if the given OSCTarget exists in the Client's dict.
        the 'address' argument can be a ((host, port) tuple), or a hostname.
        If the 'prefix' argument is given, the return-value is only True if the address and prefix match.
        """
        if type(address) in types.StringTypes:
            address = self._searchHostAddr(address) 

        if type(address) == types.TupleType:
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except socket.error:
                pass
            address = (host, port)

            if address in self.targets.keys():
                if prefix == None:
                    return True
                elif prefix == self.targets[address][0]:
                    return True

        return False
OSC2.py 文件源码 项目:pyOSC3 作者: Qirky 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def getOSCTarget(self, address):
        """Returns the OSCTarget matching the given address as a ((host, port), [prefix, filters]) tuple.
        'address' can be a (host, port) tuple, or a 'host' (string), in which case the first matching OSCTarget is returned
        Returns (None, ['',{}]) if address not found.
        """
        if type(address) in types.StringTypes:
            address = self._searchHostAddr(address) 

        if (type(address) == types.TupleType): 
            (host, port) = address[:2]
            try:
                host = socket.gethostbyname(host)
            except socket.error:
                pass
            address = (host, port)

            if (address in self.targets.keys()):
                try:
                    (host, _, _) = socket.gethostbyaddr(host)
                except socket.error:
                    pass

                return ((host, port), self.targets[address])

        return (None, ['',{}])
connectionpool.py 文件源码 项目:python- 作者: secondtonone1 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
sensor_backlog_aggregate.py 文件源码 项目:cbapi-examples 作者: cbcommunity 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def output(data, udp):
    """
    output the sensor backlog data, in JSON format
    always output to stdout
    output to udp if so specified
    """
    print data

    if udp is None:
        return

    try:
        sockaddr_components = udp.split(':')
        ip = socket.gethostbyname(sockaddr_components[0])
        port = int(sockaddr_components[1])
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
        sock.sendto(data + '\n', (ip, port))
    except Exception, e:
        print e

    return
sensor_backlog_individual.py 文件源码 项目:cbapi-examples 作者: cbcommunity 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def output(data, udp):
    """
    output the sensor backlog data, in JSON format
    always output to stdout
    output to udp if so specified
    """
    print data

    if udp is None:
        return

    try:
        sockaddr_components = udp.split(':')
        ip = socket.gethostbyname(sockaddr_components[0])
        port = int(sockaddr_components[1])
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
        sock.sendto(data + '\n', (ip, port))
    except Exception, e:
        print e

    return
connectionpool.py 文件源码 项目:my-first-blog 作者: AnkurBegining 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        host = _ipv6_host(host).lower()

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
connectionpool.py 文件源码 项目:my-first-blog 作者: AnkurBegining 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
io_helpers.py 文件源码 项目:core-framework 作者: RedhawkSDR 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def getStreamDef( self, name=None, hostip=None, pkts=1000, block=True, returnSddsAnalyzer=True):
        # grab data if stream definition is available 
        sdef =None
        aid=name
        if not aid:
            if len(self._streamdefs) ==  0:
                raise Exception("No attachment have been made, use grabData or call attach")

            aid = self._streamdefs.keys()[0]
            print "Defaults to first entry, attach id = ", aid
            sdef = self._streamdefs[aid]
        else:
            sdef = sefl._streamdefs[aid]

        if not sdef:
            raise Exception("No SDDS stream definition for attach id:" + aid )

        if not hostip:
            hostip = _socket.gethostbyname(_socket.gethostname())

        return self.getData( sdef.multicastAddress, hostip, sdef.port, packets, block=block, returnSDDSAnalyzer=returnSDDSAnalyzer)
ip.py 文件源码 项目:charm-swift-proxy 作者: openstack 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_host_ip(hostname, fallback=None):
    """
    Resolves the IP for a given hostname, or returns
    the input if it is already an IP.
    """
    if is_ip(hostname):
        return hostname

    ip_addr = ns_query(hostname)
    if not ip_addr:
        try:
            ip_addr = socket.gethostbyname(hostname)
        except Exception:
            log("Failed to resolve hostname '%s'" % (hostname),
                level=WARNING)
            return fallback
    return ip_addr
ip.py 文件源码 项目:charm-swift-proxy 作者: openstack 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def get_host_ip(hostname, fallback=None):
    """
    Resolves the IP for a given hostname, or returns
    the input if it is already an IP.
    """
    if is_ip(hostname):
        return hostname

    ip_addr = ns_query(hostname)
    if not ip_addr:
        try:
            ip_addr = socket.gethostbyname(hostname)
        except Exception:
            log("Failed to resolve hostname '%s'" % (hostname),
                level=WARNING)
            return fallback
    return ip_addr
multipartform.py 文件源码 项目:HeaTDV4A 作者: HeaTTheatR 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def choose_boundary():
    global _prefix

    if _prefix is None:
        hostid = socket.gethostbyname(socket.gethostname())

        try:
            uid = `os.getuid()`
        except:
            uid = '1'
        try:
            pid = `os.getpid()`
        except:
            pid = '1'

        _prefix = hostid + '.' + uid + '.' + pid

    timestamp = '%.3f' % time.time()
    seed = `random.randint(0, 32767)`
    return _prefix + '.' + timestamp + '.' + seed
mimetools.py 文件源码 项目:kinect-2-libras 作者: inessadl 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def choose_boundary():
    """Return a string usable as a multipart boundary.

    The string chosen is unique within a single program run, and
    incorporates the user id (if available), process id (if available),
    and current time.  So it's very unlikely the returned string appears
    in message text, but there's no guarantee.

    The boundary contains dots so you have to quote it in the header."""

    global _prefix
    import time
    if _prefix is None:
        import socket
        try:
            hostid = socket.gethostbyname(socket.gethostname())
        except socket.gaierror:
            hostid = '127.0.0.1'
        try:
            uid = repr(os.getuid())
        except AttributeError:
            uid = '1'
        try:
            pid = repr(os.getpid())
        except AttributeError:
            pid = '1'
        _prefix = hostid + '.' + uid + '.' + pid
    return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())


# Subroutines for decoding some common content-transfer-types
sensor_backlog_aggregate.py 文件源码 项目:cbapi-python 作者: carbonblack 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def output(data, udp):
    """
    output the sensor backlog data, in JSON format
    always output to stdout
    output to udp if so specified
    """
    print data

    if udp is None:
        return

    try:
        sockaddr_components = udp.split(':')
        ip = socket.gethostbyname(sockaddr_components[0])
        port = int(sockaddr_components[1])
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
        sock.sendto(data + '\n', (ip, port))
    except Exception, e:
        print e

    return
sensor_backlog_individual.py 文件源码 项目:cbapi-python 作者: carbonblack 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def output(data, udp):
    """
    output the sensor backlog data, in JSON format
    always output to stdout
    output to udp if so specified
    """
    print data

    if udp is None:
        return

    try:
        sockaddr_components = udp.split(':')
        ip = socket.gethostbyname(sockaddr_components[0])
        port = int(sockaddr_components[1])
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
        sock.sendto(data + '\n', (ip, port))
    except Exception, e:
        print e

    return
connectionpool.py 文件源码 项目:googletranslate.popclipext 作者: wizyoung 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        host = _ipv6_host(host).lower()

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
pyflooder.py 文件源码 项目:PyFlooder 作者: D4Vinci 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def attack():

    ip = socket.gethostbyname( host )
    global n
    msg=str(string.letters+string.digits+string.punctuation)
    data="".join(random.sample(msg,5))
    dos = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        n+=1
        dos.connect((ip, port))
        dos.send( "GET /%s HTTP/1.1\r\n" % data )
        print "\n "+time.ctime().split(" ")[3]+" "+"["+str(n)+"] #-#-# Hold Your Tears #-#-#"

    except socket.error:
        print "\n [ No connection! Server maybe down ] "

    dos.close()
connectionpool.py 文件源码 项目:Projects 作者: it2school 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        host = _ipv6_host(host).lower()

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
connectionpool.py 文件源码 项目:Flask_Blog 作者: sugarguo 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
connectionpool.py 文件源码 项目:pip-update-requirements 作者: alanhamlett 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        host = _ipv6_host(host).lower()

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
connectionpool.py 文件源码 项目:aws-waf-security-automation 作者: cerbo 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def is_same_host(self, url):
        """
        Check if the given ``url`` is a member of the same host as this
        connection pool.
        """
        if url.startswith('/'):
            return True

        # TODO: Add optional support for socket.gethostbyname checking.
        scheme, host, port = get_host(url)

        # Use explicit default port for comparison when none is given
        if self.port and not port:
            port = port_by_scheme.get(scheme)
        elif not self.port and port == port_by_scheme.get(scheme):
            port = None

        return (scheme, host, port) == (self.scheme, self.host, self.port)
mujson_mgr.py 文件源码 项目:shadowsocksR-b 作者: hao35954514 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def getipaddr(self, ifname='eth0'):
        import socket
        import struct
        ret = '127.0.0.1'
        try:
            ret = socket.gethostbyname(socket.getfqdn(socket.gethostname()))
        except:
            pass
        if ret == '127.0.0.1':
            try:
                import fcntl
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                ret = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])
            except:
                pass
        return ret
ip.py 文件源码 项目:charm-heat 作者: openstack 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def get_host_ip(hostname, fallback=None):
    """
    Resolves the IP for a given hostname, or returns
    the input if it is already an IP.
    """
    if is_ip(hostname):
        return hostname

    ip_addr = ns_query(hostname)
    if not ip_addr:
        try:
            ip_addr = socket.gethostbyname(hostname)
        except Exception:
            log("Failed to resolve hostname '%s'" % (hostname),
                level=WARNING)
            return fallback
    return ip_addr
ip.py 文件源码 项目:charm-keystone 作者: openstack 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def get_host_ip(hostname, fallback=None):
    """
    Resolves the IP for a given hostname, or returns
    the input if it is already an IP.
    """
    if is_ip(hostname):
        return hostname

    ip_addr = ns_query(hostname)
    if not ip_addr:
        try:
            ip_addr = socket.gethostbyname(hostname)
        except Exception:
            log("Failed to resolve hostname '%s'" % (hostname),
                level=WARNING)
            return fallback
    return ip_addr


问题


面经


文章

微信
公众号

扫码关注公众号