开发者

Determine local connectivity in python

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 us开发者_如何学JAVAeful. Any other ideas would be appreciated, thanks!


localhost should always return 127.0.0.1 in 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.Popen to run ifconfig and parse the results (it's spelled ipconfig in Windows) -- not ideal, but pretty practical, IMNSHO;-).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜