How to Test Webserver Availability using Python

February 4, 2017 | Views: 8769

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

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.

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. Very good! Can I copy, adapt and share?

  2. I guess there is an error in this script, no? 🙂

Page 2 of 2«12
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge


We recommend always using caution when following any link

Are you sure you want to continue?