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