Ready to Start Your Career?

How to Test Webserver Availability using Python

Amine Essiraj - 4m1n3 3551r4j's profile image

By: Amine Essiraj - 4m1n3 3551r4j

February 4, 2017

python network webserver

Hello Cybrary Community,

Today I’ll show you how to test the availability of a webserver using Python:

Let’s start writing our script.

import socket, sys

if len(sys.argv) != 2:

    print >>sys.stderr, 'usage: python <>'


example = sys.argv[1]


    infolist = socket.getaddrinfo(

        example, 'www', 0, socket.SOCK_STREAM, 0,

        socket.AI_ADDRCONFIG | socket.AI_V4MAPPED | socket.AI_CANONNAME,


except socket.gaierror, e:

    print 'Name service failure:', e.args[1]


info = infolist[0]  

socket_args = info[0:3]

address = info[4]

s = socket.socket(*socket_args)



except socket.error, e:

    print 'Network failure:', e.args[1]


    print 'Success: host', info[3], 'is listening on port 80'

An example of the script execution :

  • #python
  • Success: host is listening on port 80
  • #python www.inexistingwebserver.hi
  • Name service failure: getaddrinfo failed
  • #python
  • Network failure: Une tentative de connexion a ÚchouÚ car le parti connectÚ nÆa pas rÚpondu convenablement au-delÓ dÆune certaine durÚe ou une connexion Útablie a ÚchouÚ car lÆh¶te de connexion nÆa pas rÚpondu
Made with Love & Python.For more information, you can visit and search different topics, such as "Web3 Interface" and many others.
Schedule Demo