Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:46:24+00:00 2026-06-14T21:46:24+00:00

So, I ahve this list of IP’s in a CSV file. Only one column,

  • 0

So, I ahve this list of IP’s in a CSV file. Only one column, if I cat the file they all appear on different lines and the file command tells me it is ASCII text.

However, when I try to loop though the file and resolve the addesses for the different IP:s I get the error “socket.herror: [Errno 1] Unknown host”.

For some reason the value of the cell isnt properly a string, and I have trouble converting it to one.

from string import rstrip
from socket import gethostbyaddr

csv_file = open('csv_list.csv', "r")

for line in csv_file:
    dns_name = gethostbyaddr(str(line.rstrip('\n')))
    print "IP: " + line.rstrip('\n') + "DNS Name:" + dns_name[0]

Is there any way around this? I have been thinking about converting the file to a plain textfile, adding all the values from the file to a list so far but I am not sure what the best solution would be.

Anyone have any ideas?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:46:25+00:00Added an answer on June 14, 2026 at 9:46 pm

    Your problem is not the reading of the file (this can be optimized using with ..., too) but that one IP cannot be looked up reversely. The gethostbyaddr function throws an exception in that case.

    I’ve made up your sample a bit and now it reports errors not as a crash with an exception but prints out a message.

    from string import rstrip
    from socket import gethostbyaddr
    
    with open('csv_list.csv', 'r') as csv_file:
        for line in csv_file:
            ip = line.strip()
            try:
                dns_name = gethostbyaddr(ip)
                print "IP: %s, DNS Name: %s" % (ip, dns_name[0])
            except Exception, e:
                print "IP: %s, DNS lookup error: %s" % (ip, e)
    

    For example for this list of IPs:

    1.2.3.4
    8.8.8.8
    4.4.4.4
    bad IP
    1.2.3
    

    it prints

    IP: 1.2.3.4, DNS lookup error: [Errno 1] Unknown host
    IP: 8.8.8.8, DNS Name: google-public-dns-a.google.com
    IP: 4.4.4.4, DNS lookup error: [Errno 1] Unknown host
    IP: bad IP, DNS lookup error: [Errno 8] nodename nor servname provided, or not known
    IP: 1.2.3, DNS lookup error: [Errno 1] Unknown host
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am filtering the all list which ahve same lat,long in one list and
I ahve a html file, in which I include .js file. This file contains
I have a php file that acts as a gatekeeper for all the files
I am sure this should be easy but I ahve been going crazy trying
i ahve a class named FiniteStateMachine declared as below header file : FiniteStateMachine.h class
I ahve a multi tenant Django database. All my multi tenant enabled models import
I ahve this code, the trouble is few of the images are having names,
I ahve this code in asp that has a paragraph and a link in
I ahve apache httpd 2.2.15 (Unix) using mod_rewrite I have this rewrite goingon: RewriteEngine
I'm running ASP.NET 4.0 WEBFORMS This problem bugs me... I ahve read up on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.