How can I lookup hostname using the IP address with a timeout in Python?

0 votes
Hi all!

My question is - How could I search for a hostname considering that I already have an IP address corresponding to that machine?

To elaborate, is it possible to specify a custom timeout in a case where no reverse DNS entry is present?

I am actually going about doing this because I want to keep the code efficient and fast. I am confused as to what I am missing or if there is a better way to go about doing this?

All help appreciated!
Feb 6, 2019 in Python by Anirudh
• 2,080 points
429 views

1 answer to this question.

0 votes

Good question. I actually was stuck with something like this in the past.

Here's the solution. Check out the following piece of code:

>>> import socket
>>> socket.gethostbyaddr("69.59.196.211")
('stackoverflow.com', ['211.196.59.69.in-addr.arpa'], ['69.59.196.211'])

You can also implement the timeout on the particular function as you desire.

Hope this helped!

answered Feb 6, 2019 by Nymeria
• 3,520 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

how can i extact all the links from a website using python and save it in a csv file ?

Hi, @Shubham, Web scraping is the technique to ...READ MORE

answered Jun 16 in Python by Gitika
• 37,660 points
230 views
+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,480 points
23,996 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,780 points
6,173 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,371 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,013 views
0 votes
1 answer

How can I parse a YAML file in Python?

Read & Write YAML files with Python ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,520 points
7,376 views