I have tried to test the network connectivity using the following script,but the response shows active irrespective of the actual status. Note: tested for local and public ip as well.
import sys,os,socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create a TCP/IP socket
server_ip = '192.168.x.x'
rep = os.system('ping ' + server_ip)
if rep == 0:
print ('server is up ')
else:
print ('server is down')