Пример #1
0
def get_lan_ip():
    try:
        from objc_util import ObjCClass
        NSHost = ObjCClass('NSHost')
        addresses = []
        for address in NSHost.currentHost().addresses():
            address = str(address)
            if 48 <= ord(address[0]) <= 57 and address != '127.0.0.1':
                addresses.append(address)
        return '   '.join(addresses)

    except ImportError:
        return ''
Пример #2
0
    def get_ip(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("8.8.8.8", 80))
            ip = s.getsockname()[0]
            s.close()
            return ip
        except:
            #offline
            if pythonista:
                try:
                    from objc_util import ObjCClass
                    NSHost = ObjCClass('NSHost')
                    addresses = []
                    for address in NSHost.currentHost().addresses():
                        address = str(address)
                        if 48 <= ord(
                                address[0]) <= 57 and address != '127.0.0.1':
                            addresses.append(address)
                    #return '   '.join(addresses)
                    return addresses[-1]

                except ImportError:
                    return ''
            else:
                #PC
                import subprocess
                try:
                    result = subprocess.check_output(
                        'ifconfig en0 |grep -w inet', shell=True)
                except:
                    try:
                        result = subprocess.check_output(
                            'ifconfig eth0 |grep -w inet', shell=True)
                    except:
                        return False
                ip = ''
                if result:
                    strs = result.split('\n')
                    for line in strs:
                        # remove \t, space...
                        line = line.strip()
                        if line.startswith('inet '):
                            a = line.find(' ')
                            ipStart = a + 1
                            ipEnd = line.find(' ', ipStart)
                            if a != -1 and ipEnd != -1:
                                ip = line[ipStart:ipEnd]
                                break

                    return ip
Пример #3
0
    def get_ip(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(("8.8.8.8", 80))
            ip = s.getsockname()[0]
            s.close()
            return ip
        except:
            #offline
            try:
                from objc_util import ObjCClass
                NSHost = ObjCClass('NSHost')
                addresses = []
                for address in NSHost.currentHost().addresses():
                    address = str(address)
                    if 48 <= ord(address[0]) <= 57 and address != '127.0.0.1':
                        addresses.append(address)
                #return '   '.join(addresses)
                return addresses[-1]

            except ImportError:
                return ''