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 in Python by Anirudh
• 2,070 points
69 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 by Nymeria
• 3,520 points

Related Questions In Python

+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,350 points
5,710 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,720 points
1,704 views
0 votes
1 answer

How can I define a multidimensional array in python using ctype?

Here's one quick-and-dirty method: >>> A = ((ctypes.c_float ...READ MORE

answered Oct 9, 2018 in Python by ariaholic
• 7,340 points
308 views
0 votes
1 answer

How can I rename multiple files in a certain directory using Python?

Use os.rename(src, dst) to rename or move a file ...READ MORE

answered Nov 23, 2018 in Python by ariaholic
• 7,340 points
119 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 230 views
+4 votes
6 answers
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
1,818 views