0
此代码能够检测互联网是否断开连接或连接,并且大部分工作正如我测试过的。问题是它偶尔会说互联网断开连接,然后说互联网连接。当我浏览网页,观看视频或其他内容时,发生过很多次,重要的是我知道互联网正在发挥作用。网络检查器给出错误的底片python
我知道代码有点乱。什么导致这些虚假断线?
import win32com.client as w
import socket
s = w.Dispatch("SAPI.SpVoice")
try:
socket.setdefaulttimeout(5)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53))
a = True
except Exception:
a = False
pass
while a == True:
while True:
try:
socket.setdefaulttimeout(5)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53))
s.Speak("Internet connected")
break
except Exception:
continue
while True:
try:
socket.setdefaulttimeout(5)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53))
continue
except Exception:
s.Speak("Internet disconnected")
break
while a == False:
while True:
try:
socket.setdefaulttimeout(5)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53))
continue
except Exception:
s.Speak("Internet disconnected")
break
while True:
try:
socket.setdefaulttimeout(5)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53))
s.Speak("Internet connected")
except Exception:
continue
谢谢。我没有想到我会在谷歌上的负载。至于捕捉异常,我不认为它是抛出任何。脚本停止运行,如果没有正确捕获异常?我在后台运行这个脚本好几个小时,但它并没有停止运行。 – Krishi