Home 0P3N Blog How to Test Webserver Availability using Python
Ready to Start Your Career?
Create Free Account
By: Amine Essiraj - 4m1n3 3551r4j
February 4, 2017

How to Test Webserver Availability using Python

By: Amine Essiraj - 4m1n3 3551r4j
February 4, 2017
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 test_webserver.py <www.example.com>'

    sys.exit(2)

example = sys.argv[1]

try:

    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]

    sys.exit(1)

info = infolist[0]  

socket_args = info[0:3]

address = info[4]

s = socket.socket(*socket_args)

try:

    s.connect(address)

except socket.error, e:

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

else:

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


An example of the script execution :

  • #python test_webserver.py www.google.com
  • Success: host www.google.com is listening on port 80
  • #python test_webserver.py www.inexistingwebserver.hi
  • Name service failure: getaddrinfo failed
  • #python test_webserver.py 8.8.8.8
  • 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 python.org and search different topics, such as "Web3 Interface" and many others.
Request Demo

Build your Cybersecurity or IT Career

Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry