-6

I am looking for a python script without using any external module to download to check whether the server is up or not. The user input will be the server name (not the url) Example : Server name - X123456 Final output - Server is Running

Champ
  • 3
  • 2
  • 1
    Possible duplicate of [Python Function to test ping](https://stackoverflow.com/questions/26468640/python-function-to-test-ping) – Chamath Feb 17 '18 at 05:44

1 Answers1

1

You can use urllib2 and socket module for the task.

import socket
from urllib2 import urlopen, URLError, HTTPError

socket.setdefaulttimeout( 23 )  # timeout in seconds

url = 'http://google.com/'
try :
    response = urlopen( url )
except HTTPError, e:
    print 'The server couldn\'t fulfill the request. Reason:', str(e.code)
except URLError, e:
    print 'We failed to reach a server. Reason:', str(e.reason)
else :
    html = response.read()
    print 'got response!'
    # do something, turn the light on/off or whatever
The Dead Mayan
  • 357
  • 3
  • 10