Home > OS >  socket.gethostbyname [Errno -2] Name or service not known
socket.gethostbyname [Errno -2] Name or service not known

Time:10-21

I was trying to check a few domain names but even some common one are returning this

the error occurs in "df['IPaddr'] = socket.gethostbyname(DN)"

socket.gethostbyname [Errno -2] Name or service not known

So I tried to try: but most of them are failing!

checked domain

Unexpected error:

AMD.com

Unexpected error:

AOL.com

import whois
import matplotlib.pyplot as plt
import numpy as np  
import pandas as pd  
import socket
import os
import csv 
import datetime
import time
import requests
from ipwhois import IPWhois
from urllib import request
from ipwhois.utils import get_countries
import tldextract
from ipwhois.utils import get_countries
countries = get_countries(is_legacy_xml=True)
from ipwhois.experimental import bulk_lookup_rdap
from ipwhois.hr import (HR_ASN, HR_ASN_ORIGIN, HR_RDAP_COMMON, HR_RDAP, HR_WHOIS, HR_WHOIS_NIR)
countries = get_countries(is_legacy_xml=True)
import ipaddress

df = pd.read_csv('a.csv')

#TimeOut Setting
s = socket.socket()
s.settimeout(10)

#Date Processing Function

def check_date_type(d):
    if type(d) is datetime.datetime:
        return d
    if type(d) is list:
        return d[0]
for index,row in df.iterrows():
  try:


      DN = df.iloc[index]['Domains']
      print(DN)
      df['IPaddr'] = socket.gethostbyname(DN)
      ipwhois = IPWhois(df['IPaddr'], allow_permutations=True).lookup_whois()
      if (ipwhois):
        df['IPcity'] = ipwhois['nets'][0]['city']
        df['ASNumber'] = ipwhois['asn']
        df['NetAddr'] = ipwhois['nets'][0]['address']
        df['NetCity'] = ipwhois['city']
        df['NetPostCode'] = ipwhois['nets'][0]['postal_code']
  except:
    print("Unexpected error:")
    pass

df.to_csv('a1.csv', index=False)

CodePudding user response:

allow_permutations=True doesn't look like a valid parameter for IPWhois. Because you're using try you might not be seeing the TypeError:

TypeError: __init__() got an unexpected keyword argument 'allow_permutations'

Once I remove that parameter, everything works as expected for me:

from ipwhois import IPWhois
import socket
DN = 'AMD.com'

ip = socket.gethostbyname(DN)
ipwhois = IPWhois(ip).lookup_whois()

print(ip)
print(ipwhois)
23.50.124.85
{'nir': None, 'asn_registry': 'arin', 'asn': '16625', 'asn_cidr': '23.50.124.0/22', 'asn_country_code': 'US', 'asn_date': '2011-05-16', 'asn_description': 'AKAMAI-AS, US', 'query': '23.50.124.85', 'nets': [{'cidr': '23.32.0.0/11, 23.64.0.0/14', 'name': 'AKAMAI', 'handle': 'NET-23-32-0-0-1', 'range': '23.32.0.0 - 23.67.255.255', 'description': 'Akamai Technologies, Inc.', 'country': 'US', 'state': 'MA', 'city': 'Cambridge', 'address': '145 Broadway', 'postal_code': '02142', 'emails': ['[email protected]', '[email protected]'], 'created': '2011-05-16', 'updated': '2012-03-02'}], 'raw': None, 'referral': None, 'raw_referral': None}

If you're still having trouble, there may be a DNS or routing issue causing problems. A quick way to isolate whether your local environment is the issue or not is to test your code snippet in something like Google Colab since the runtime will be on Google servers: https://colab.research.google.com/

If neither of these suggestions work, you'll have better luck removing the try statement or print the exception to the console and post the error you receive here:

try:
  ip = socket.gethostbyname(DN)
  ipwhois = IPWhois(ip, allow_permutations=True).lookup_whois()
  print(ip)
  print(ipwhois)

except Exception as e:
  print(e) 
  • Related