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

Community
  • 1
  • 1
RPDeshaies
  • 1,826
  • 1
  • 24
  • 29

2 Answers2

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)
Peter Party Bus
  • 2,326
  • 1
  • 14
  • 15
  • 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 – RPDeshaies Oct 28 '13 at 15:21
  • 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? – Peter Party Bus Oct 28 '13 at 15:34
  • No problem, Yes this is what I would like to do. I would like this process to be the fastest as possible – RPDeshaies Oct 28 '13 at 15:48
  • 1
    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: http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib Other than that I think Prahalad Deshpandes post has the correct idea as far as best practices go. Goodluck. Cheers. – Peter Party Bus Oct 28 '13 at 18:20
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

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • 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. – RPDeshaies Oct 28 '13 at 15:52
  • 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. – Prahalad Deshpande Oct 28 '13 at 16:59
  • 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) – RPDeshaies Oct 29 '13 at 01:32
  • And you are sure that scapy has been added to your PYTHONPATH? – Prahalad Deshpande Oct 29 '13 at 04:48
  • 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 ? – RPDeshaies Oct 29 '13 at 14:04
  • Yes, you can use python tools like PIP or easy_install that will manage package installations for you. Refer http://docs.python.org/2/tutorial/modules.html for details – Prahalad Deshpande Oct 30 '13 at 03:48
  • Even if I installed the scapy software I still have the same two errors. Do you have any idea of what caused that ? If no, do you have another way, instead of neighbourhood, to list all computer hostname and ip adress from scapy ? – RPDeshaies Oct 30 '13 at 12:29
  • @Tareck117 I am sure, this is a simple configuration problem related to the PYTHONPATH. Basically PYTHONPATH specifies the path locations where the python interpreter will look for modules while bootstrapping your script. Using what tool did you install the scapy software? – Prahalad Deshpande Oct 30 '13 at 16:59
  • Directly with the windows command prompt using the setup.py install from the scapy folder – RPDeshaies Oct 31 '13 at 02:33
  • Isn't Scapy for anything but local addresses? – vallentin Mar 23 '16 at 04:05