0

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')
  • What's the purpose of this line: `s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Create a TCP/IP socket` ? Also what *OS* are you running your code on? – CristiFati Jul 02 '18 at 09:15
  • Hi @CristiFati the purpose the statement is to connect certain websites at specific ports for internal use. I am currently using windows 8.1 – Karan Chaudhary Jul 02 '18 at 09:31
  • You're testing the exit code of the ping command, which unfortunately doesn't accurately reflect whether the target system is actually responding. See the answers to these questions for more explanation: https://stackoverflow.com/q/9329749/1076479 and https://stackoverflow.com/q/27746132/1076479 – Gil Hamilton Jul 02 '18 at 19:01
  • thanks @GilHamilton – Karan Chaudhary Jul 03 '18 at 09:27

0 Answers0