我需要写一个检查互联网连接的程序。如果互联网连接可用,绿灯亮,否则红灯亮。
请提供建议/想法。
提前致谢!
答案 0 :(得分:0)
尝试连接到谷歌:
import socket
try:
socket.create_connection(('8.8.8.8',80))
print 'connected to google'
except socket.error as msg:
print 'there is no connection'
如果它返回异常,则没有互联网连接