8

Just to be clear: I just started Python 2 weeks ago but I'm a C#, ASP, PHP, JavaScript developer.

I just started a new project with Python and PyQt and I want my project to be a server that will be able to communicate with other instance of this server on other computers.

So, I need to get computers IP address and host name.

First, I thought about using the MSDOS "net view" command but this was before trying this command at my job and seeing that this could take around ~15s (which I think is too slow).

I had this idea from: https://stackoverflow.com/a/15497083/1598891

Also, the "net view" command can return computers that are no longer on the network and cause exception when trying to get their IP addresses by host name (which also slows down the process each time a computer cannot be accessed).

My Question: Is there a way to get all computers IP and Host Name on the Local network without passing by net view on Windows because net view could be a slow operation?

Also, is there a way to only get computers that are running my application? (so it would be faster)

Thanks in advance

2 Answers 2

23

For the hostname and ip of the localhost you could use the socket module and gethostname, and gethostbyname methods:

import socket

hostname = socket.gethostname()
IP = socket.gethostbyname(hostname)
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't answer my question, my question is : If there a way to get all computers IP and Host Name on the network without passing by net view on Windows because net view could be a slow operation
I apologize. So you are looking for all the boxes on your network that are running your app, and the check has to be done in around under ~15 seconds?
No problem, Yes this is what I would like to do. I would like this process to be the fastest as possible
Well it depends how you are detecting your app, if you have an open socket for example you can just for loop with the socket module and check for open ports on the network. Here are some examples: stackoverflow.com/questions/166506/… Other than that I think Prahalad Deshpandes post has the correct idea as far as best practices go. Goodluck. Cheers.
5

If you want to get the IP address of the host on which the python script is running, then the answer provided by user2096338 is the way to go.

In order to discover all the computers on a network within a LAN you can use scapy. https://github.com/bwaldvogel/neighbourhood/blob/master/neighbourhood.py is a link that I found while browsing a similar SO question; which discovers all computers within a network (LAN).

If you want to ensure that the above script returns only those computers that are running your server - then there are multiple ways to do so, listed below in order of portablility

  1. Whenever you install your server on a host, then register itself into a central database which can then be queried by a new instance of your service to identify all computers where the peer servers are running
  2. Use WMI (since you are on Windows) to query all processes running on the peer system anc check if your server process is one amongst them.
  3. Your server will keep a probing port open and a new server installation will ping to the server on this probing port for each of the hosts in the network. If it gets a response, then this computer is running the server process.

In my opinion, method 1 is the safest and portable, since it prevents the opening up of unnecessary ports( like method 3) which are security holes (depending on whose context your python script runs). It also does not depend on the availability of the WMI service on the remote hosts ( some systems may have WMI disabled).

Hope this helps

10 Comments

It helps very well. Does neighbourhood.py works on Windows ? Also, I think, since this is only a little application and I have no intention to use a database for it, that I'll probably use the method 3. But first I'll read more about method 2. Thanks for you help. I'll mark your answer as the "Accepted Answer" once I'll have tested scapy and neighbourhood this evening.
Sure thing :). Thanks for the feedback. Yes and as per my knowledge this neighbourhood.py must work on Windows too since it uses scapy which is also available for Windows.
I've added Scapy to my project and neightbourhood.py too, but when i'm running it, I'm getting the following error : ImportError: cannot import name debug For the line : import scapy.layers.l2 Also, Eclipse don't seems to recognize the following line (in neihbourhood, line 51) for network, netmask, _, interface, address in scapy.config.conf.route.routes: (undefined variable for import route)
And you are sure that scapy has been added to your PYTHONPATH?
Since I'm new to Python, I did not know that I needed to install the module. I thought that I only needed to import the code into my project. I'll try to install it (with cmd : python setup.py install ?) this evening. Sorry for bothering you for such a basic problem. Do I need to install every python module in Python that I want to use in my projects ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.