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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:15:23+00:00 2026-05-27T01:15:23+00:00

I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python,

  • 0

I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python, which returns ‘Unknown Host’ for some values, but using dig for the same ip returns the Hostname. Also, dig seems to be significantly faster than using python module, is there any specific reasons for that?

import socket

# This returns 'Unknown Host' 
name, alias, addresslist = socket.gethostbyaddr('114.143.51.197')
  • 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-05-27T01:15:24+00:00Added an answer on May 27, 2026 at 1:15 am

    From the comments…

    whereas dig -x 114.143.51.197+short gives me the hostname.

    I’m sorry, but you are mistaken. 114.143.51.197 does not have a PTR record… therefore socket.gethostbyaddr() should throw an error… To process this use-case correctly, add a try / except clause that traps socket.herror

    >>> def dns_ptr_lookup(addr):
    ...     try:
    ...         return socket.gethostbyaddr(addr)
    ...     except socket.herror:
    ...         return None, None, None
    ...
    >>> # At this time, 4.2.2.2 has a valid PTR
    >>> name,alias,addresslist = dns_ptr_lookup('4.2.2.2')
    >>> print(name)
    vnsc-bak.sys.gtei.net
    >>>
    >>> # At this time, 114.143.51.197 does NOT have a valid PTR
    >>> name,alias,addresslist = dns_ptr_lookup('114.143.51.197')
    >>> print(name)
    None
    >>>
    

    DNS reverse lookup for 114.143.51.197… note that it does not have a valid PTR record

    [mpenning@Bucksnort ~]$ dig @8.8.8.8 -x 114.143.51.197
    
    ; <<>> DiG 9.6-ESV-R4 <<>> @8.8.8.8 -x 114.143.51.197
    ; (1 server found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 4735
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
    
    ;; QUESTION SECTION:
    ;197.51.143.114.in-addr.arpa.   IN      PTR
    
    ;; AUTHORITY SECTION:
    114.in-addr.arpa.       1800    IN      SOA     ns1.apnic.net. read-txt-record-of-zone-first-dns-admin.apnic.net. 17812 7200 1800 604800 172800
    
    ;; Query time: 182 msec
    ;; SERVER: 8.8.8.8#53(8.8.8.8)
    ;; WHEN: Tue Nov 22 05:11:36 2011
    ;; MSG SIZE  rcvd: 134
    
    [mpenning@Bucksnort ~]$ python
    Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import socket
    >>> socket.gethostbyaddr('114.143.51.197')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    socket.herror: (1, 'Unknown host')
    >>>
    

    This is what a valid PTR record should look like…

    [mpenning@Bucksnort ~]$ dig -x 4.2.2.2
    
    ; <<>> DiG 9.6-ESV-R4 <<>> -x 4.2.2.2
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61856
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 3, ADDITIONAL: 1
    
    ;; QUESTION SECTION:
    ;2.2.2.4.in-addr.arpa.          IN      PTR
    
    ;; ANSWER SECTION:
    2.2.2.4.in-addr.arpa.   86400   IN      PTR     vnsc-bak.sys.gtei.net.
    
    ;; AUTHORITY SECTION:
    2.4.in-addr.arpa.       86400   IN      NS      dnsauth2.sys.gtei.net.
    2.4.in-addr.arpa.       86400   IN      NS      dnsauth1.sys.gtei.net.
    2.4.in-addr.arpa.       86400   IN      NS      dnsauth3.sys.gtei.net.
    
    ;; ADDITIONAL SECTION:
    dnsauth1.sys.gtei.net.  1800    IN      A       4.2.49.2
    
    ;; Query time: 308 msec
    ;; SERVER: 127.0.0.1#53(127.0.0.1)
    ;; WHEN: Tue Nov 22 05:10:16 2011
    ;; MSG SIZE  rcvd: 158
    
    [mpenning@Bucksnort ~]$ python
    Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import socket
    >>> socket.gethostbyaddr('4.2.2.2')
    ('vnsc-bak.sys.gtei.net', [], ['4.2.2.2'])
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to reverse geocode a series of lat/long co-ordinates using the Virtual
Im trying to reverse the order of the following linked list, I've done so,
I am trying to reverse a C style string using the following simple program.
I am trying to get reverse DNS lookups working with the net-dns gem for
I am trying to reverse engineer pojos (using hibernate tools plugin v3.2.4x in eclipse
I'm trying to reverse-engineer a program that does some basic parsing: text in, text
I was trying to implement the reverse function of itertools.izip on Python 2.7.1. The
I am trying to reverse-engineer a website I don't own, figuring out how some
I have some code and I'm trying to do my For loop in reverse
I am trying to reverse order of given numbers in python.The problem says that

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.