Snippets

Alexander Hanel resolve_dotbit_domain

Created by Alexander Hanel
def resolve_dotbit_domain(domain):
    """ resolves .bit domains using opennic api """
    try:
        import requests
        from dns import resolver
    except Exception as e:
        print "Exception: %s" % e
        return False, None
    # use openNic API to grab json of IP4 addresses
    api_url = "https://api.opennicproject.org/geoip/?json&ipv=4"
    response = requests.get(api_url)
    # verify valid response
    if response.status_code is not 200:
        return False, None
    ip_json = response.json()
    name_servers = []
    # add all ip to name_server
    for tt in ip_json:
        name_servers.append(tt["ip"].encode('ascii', 'ignore'))
    # configure=False will ignore OS DNS settings
    res = resolver.Resolver(configure=False)
    res.nameservers = name_servers
    try:
        answers = res.query(domain)
        for rdata in answers:
            if rdata.address:
                return True, rdata.address.encode('ascii', 'ignore')
    except Exception as e:
        print e
        return False, None

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.