All pastes #900230 Raw Edit

ks

public text v1 · immutable
·published 2008-02-11 19:35 UTC
rendered paste body
def get_ip_address(ifname):
    """Return the ip address of the specified interface name.
    @param ifname: The name of the network interface.
    @type ifname: String
    @return: The ip address.
    @rtype: String"""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),
            0x8915,  # SIOCGIFADDR
            struct.pack('256s', ifname[:15])
        )[20:24])
    except Exception,e:
        logging.error("Could not get address. Returning localhost. %s"%e)
    return "127.0.0.1"