List of IP addresses/hostnames from local network in Python

前端 未结 10 2422
暗喜
暗喜 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:56

    Try:

    import socket
    
    print ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1])
    

提交回复
热议问题