Python - 在运行相同应用程序的网络上获取计算机的 IP 地址和主机名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19636817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Get computer's IP address and host name on network running same application
提问by RPDeshaies
Just to be clear: I just started Python 2 weeks ago but I'm a C#, ASP, PHP, JavaScript developer.
需要说明的是:我两周前才开始使用 Python,但我是一名 C#、ASP、PHP、JavaScript 开发人员。
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.
我刚刚用 Python 和 PyQt 开始了一个新项目,我希望我的项目成为一个服务器,能够与其他计算机上该服务器的其他实例进行通信。
So, I need to get computers IP address and host name.
所以,我需要获取计算机的 IP 地址和主机名。
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).
首先,我考虑过使用 MSDOS“net view”命令,但这是在我的工作中尝试此命令之前,看到这可能需要大约 15 秒(我认为这太慢了)。
I had this idea from: https://stackoverflow.com/a/15497083/1598891
我有这个想法: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).
此外,“net view”命令可以返回不再处于网络上的计算机,并在尝试通过主机名获取其 IP 地址时导致异常(这也会在每次无法访问计算机时减慢进程)。
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?
我的问题:有没有办法在本地网络上获取所有计算机的 IP 和主机名,而不会net view
在 Windows 上通过,因为net view
操作可能很慢?
Also, is there a way to only get computers that are running my application?(so it would be faster)
另外,有没有办法只获取运行我的应用程序的计算机?(所以它会更快)
Thanks in advance
提前致谢
采纳答案by Prahalad Deshpande
If you want to get the IP address of the host on which the python script is running, then the answer provided by user2096338is the way to go.
如果您想获取运行python脚本的主机的IP地址,那么user2096338提供的答案就是要走的路。
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.pyis a link that I found while browsing a similarSO question; which discovers all computers within a network (LAN).
为了发现局域网内网络上的所有计算机,您可以使用scapy。https://github.com/bwaldvogel/邻居/blob/master/邻居.py是我在浏览类似的SO 问题时找到的链接;它发现网络 (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
如果你想确保上面的脚本只返回那些运行你的服务器的计算机 - 那么有多种方法可以做到,下面按可移植性的顺序列出
- 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
- 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.
- 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.
- 每当您在主机上安装服务器时,将自身注册到中央数据库中,然后您的服务的新实例可以查询该数据库,以识别运行对等服务器的所有计算机
- 使用 WMI(因为您使用的是 Windows)来查询在对等系统上运行的所有进程,并检查您的服务器进程是否是其中之一。
- 您的服务器将保持一个探测端口打开,一个新的服务器安装将在这个探测端口上为网络中的每个主机ping 到服务器。如果它得到响应,那么这台计算机正在运行服务器进程。
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).
在我看来,方法 1 是最安全和可移植的,因为它可以防止打开不必要的端口(如方法 3),这些端口是安全漏洞(取决于您的 Python 脚本运行的上下文)。它也不依赖于远程主机上 WMI 服务的可用性(某些系统可能禁用了 WMI)。
Hope this helps
希望这可以帮助
回答by Peter Party Bus
For the hostname and ip of the localhost you could use the socket module and gethostname, and gethostbyname methods:
对于 localhost 的主机名和 ip,您可以使用 socket 模块和 gethostname 以及 gethostbyname 方法:
import socket
hostname = socket.gethostname()
IP = socket.gethostbyname(hostname)