import socket def isNetOK(testserver): s = socket.socket() s.settimeout(3) try: status = s.connect_ex(testserver) if status == 0: s.close() return True else: return False except Exception as e: return False def isNetChainOK(testserver=('www.baidu.com', 443)): isOK = isNetOK(testserver) return isOK chinanet = isNetChainOK() if chinanet is False: print("当前网络不佳") exit() else: print("当前网络良好")