代码之家  ›  专栏  ›  技术社区  ›  El-Chief

网络检查器提供误报python

  •  0
  • El-Chief  · 技术社区  · 8 年前

    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
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Danielle M.    8 年前

    你正在以大约每秒100次的速度连接到谷歌的DNS服务器,至少在我的机器上是这样。如果他们偶尔拒绝你的联系,我不会感到惊讶,但我不愿意检验这个理论。也许您的机器正在以这种速度耗尽端口?我不知道windows TCP堆栈将如何处理这样的事情。

    试着在你的所有循环中加入睡眠,以确保你没有重击它们——每隔几秒钟测试一次你的连接就可以了。