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 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.

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
10 Comments
  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?

Continue
Cancel