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 9188041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:55:10+00:00 2026-06-17T19:55:10+00:00

I have a script that reads addresses from a file and looks up its

  • 0

I have a script that reads addresses from a file and looks up its hostname with socket.gethostbyaddr, however the return of this function is messy and doesn’t look right.

The line where it writes to the destination file reads:

destfile.write(str(socket.gethostbyaddr(ip)))

The results come out like this when it reads 8.8.8.8:

('google-public-dns-a.google.com', [], ['8.8.8.8])

However, I only need that first output, google-public-dns-a.google.com. I hope to have it write to the file and look like this:

8.8.8.8 resolves to google-public-dns-a.google.com

Anyone know how to split this? Can provide more code if needed.

  • 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-17T19:55:12+00:00Added an answer on June 17, 2026 at 7:55 pm

    Well, the first step is to split the one-liner up into multiple lines:

    host = socket.gethostbyaddr(ip)
    

    Now, you can do whatever you want to that. If you don’t know what you want to do, try printing out host and type(host). You’ll find that it’s a tuple of 3 elements (although in this case, you could have guessed that from the string written to the file), and you want the first. So:

    hostname = host[0]
    

    Or:

    hostname, _, addrlist = host
    

    Now, you can write that to the output:

    destfile.write('{} resolves to {}'.format(ip, hostname))
    

    Another way to discover the same information would be to look at the documentation, which says:

    Return a triple (hostname, aliaslist, ipaddrlist) where hostname is the primary host name responding to the given ip_address, aliaslist is a (possibly empty) list of alternative host names for the same address, and ipaddrlist is a list of IPv4/v6 addresses for the same interface on the same host (most likely containing only a single address).

    Or to use the built-in help in the interpreter:

    >>> help(socket.gethostbyaddr)
    gethostbyaddr(host) -> (name, aliaslist, addresslist)
    
    Return the true host name, a list of aliases, and a list of IP addresses,
    for a host.  The host argument is a string giving a host name or IP number.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that reads a list of addresses from a table and
I have script that reads remote file content and writes it to local server.
I have a Python script that reads through a text csv file and creates
I have a PHP script that reads from a pipe in blocking mode. while($input
I have a small script that reads a file. After reading a line i'm
I have a simple script of code that reads a PHP file and when
I have a written a script in PHP that reads from data from a
I have an applescript that reads data from a CSV (successfully!). After my script
I have a Perl script that uses WWW::Mechanize to read from a file and
I have a simple script that takes a subject-line and return-path from STDIN (a

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.