SoFunction
Updated on 2024-10-29

python method to determine whether a device is connected or not

This article example for you to share the python to determine whether the device is connected to the specific code, for your reference, the details are as follows

Straight to the code, it's the same method used to determine if the socket can connect or not.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket

def isNetOK(testserver):
  s=()
  (3)
  try:
    status = s.connect_ex(testserver)
    if status == 0:
      ()
      return True
    else:
      return False
  except Exception as e:
    return False

def isNetChainOK(testserver=('',443)):
  isOK = isNetOK(testserver)
  return isOK


def isNetUSAOK(testserver=('',443)):
  isOK = isNetOK(testserver)
  return isOK

def isNetYouTubeOK(testserver=('',443)):
  isOK = isNetOK(testserver)
  return isOK

def main():

  chinanet = isNetChainOK()
  print chinanet
  usanet = isNetUSAOK()
  print usanet
  youtubenet = isNetYouTubeOK()
  print youtubenet


if __name__ == '__main__':
  main()

This is the entire content of this article.