List of IP addresses/hostnames from local network in Python

前端 未结 10 2445
暗喜
暗喜 2020-12-01 00:05

How can I get a list of the IP addresses or host names from a local network easily in Python?

It would be best if it was multi-platform, but it needs to work on Mac

10条回答
  •  余生分开走
    2020-12-01 00:38

    If you know the names of your computers you can use:

    import socket
    IP1 = socket.gethostbyname(socket.gethostname()) # local IP adress of your computer
    IP2 = socket.gethostbyname('name_of_your_computer') # IP adress of remote computer
    

    Otherwise you will have to scan for all the IP addresses that follow the same mask as your local computer (IP1), as stated in another answer.

提交回复
热议问题