Python script to ping linux server

You can use urllib2 and socket module for the task.

import socket
from urllib2 import urlopen, URLError, HTTPError

socket.setdefaulttimeout( 23 )  # timeout in seconds

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 =
    print 'got response!'
    # do something, turn the light on/off or whatever

Browse More Popular Posts

Leave a Comment