How can I tell if my client system has a network connection using python? I can assume the client connected with DHCP. I can’t use lists of known reliable sites to ping to test the connection, as it needs to work in isolated networks as well as open ones.
I thought about fetching the local ip (should work, so long as it doesn’t only return loopback). But….
print socket.gethostbyaddr('localhost')
returns
('localhost', ['ip6-localhost', 'ip6-loopback'], ['::1'])
which isn’t very useful. Any other ideas would be appreciated, thanks!
localhostshould always return127.0.0.1in ip4,'::1'in ip6, so of course it’s not going to be useful — it’s the loopback interface, not the ethernet card or whatever;-).Personally, I’d use
subprocess.Popento runifconfigand parse the results (it’s spelledipconfigin Windows) — not ideal, but pretty practical, IMNSHO;-).