Sunday, January 23, 2011

Creating a network tools service page

I first wrote the following code in 2008 and it is still running. People who work in Linux are blessed with plenty of open source and free software tools for networking. Here are five tools which we use in our nettools service page:ping, whois, nslookup, traceroute, and dig.


We show the code, using the mean and lean QP web framework for future reference.
More details are avaiable from the link Digital Explorations, our main blog site.




from qp.sites.extreme.lib.config import image_dir, homeurl

__version__ = "0.0.1 2008.10.23"
__date__    = "2008.10.23"
__catalog__ = "SERV-NETTOOLS-001"
__author__  = "ernesto.adorio@gmail.com"
__url__     = homeurl + "services/nettools/"
__title__   = "NetTools Service Page"



from qp.fill.css        import BASIC_FORM_CSS
from qp.fill.directory  import Directory
from quixote.errors     import PublishError
from quixote.util       import dump_request
from qp.fill.form       import Form
from qp.fill.widget    import CheckboxWidget,StringWidget,SingleSelectWidget, StringWidget, TextWidget
from qp.pub.common      import header, footer, page, redirect
from   qp.sites.extreme.lib.uicommon import renderheader, renderfooter, processheader, processfooter
import time
import urllib



import urllib, urllib2, os

from  qp.sites.extreme.lib.webutils  import before, after
from  qp.sites.extreme.lib import config


def getstatusoutput(command):
     """
     Fredrick Lundh 
     http://mail.python.org/pipermail/python-list/2006-October/406444.html 
     """
     from subprocess import Popen, PIPE, STDOUT
     p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
     s = p.stdout.read().split("\n")
     return p.wait(), s

def isAddressOK(Address):
    """
    Check for bad addresses.
    """
    #Quickie checks.
    if " " in Address:
       return False,"Embedded blanks are not allowed inside addresses"
    if "." not in Address:
       return False
    if Address.startswith("-"): # attempt to hack?
       return False, "Addresses do not start with a dash (-)."

    #Pure numeric address?
    return True, "Ok"


class NetToolsPage(Directory):
    def get_exports(self):
        yield '', 'index', 'NetTools server', None



    def index (self):
        form  = Form(enctype = "multipart/form-data")  # enctype for file upload

 form.add(StringWidget, name = "Address", value = "www.adorio-research.org", size = 30)
        
 form.add(SingleSelectWidget, name = "service",
        value = "ping",  options=[("ping", "ping"),
        ("whois", "whois"), 
        ("nslookup", "nslookup"),
        ("traceroute","traceroute"),
        ("dig", "dig")])

                 
 form.add_hidden("time",   value = time.time()) 
        form.add_submit("submit", "submit")


 
 def render [html] ():
            renderheader(__title__)
     
            """
%sAddress or domain name
%sService command
Please wait for the output to be displayed after clicking on the submit button! """ % (form.get_widget("Address").render(), form.get_widget("service").render()) renderfooter(form, __version__, __catalog__, __author__) if not form.is_submitted(): return page('NetToolsPage', render(), style= BASIC_FORM_CSS) def process [html] (): processheader("NetTools Server") calctime_start = time.time() Address = form.get("Address") service = form.get("service") if service == "ping": command = "ping -c 5 %s" % Address else: command = "%s %s" % (service, Address) status = 0 addressOK, errormessage = isAddressOK(Address) if addressOK: status, output = getstatusoutput(command) if status == 0: """
"""
                  for line in output:
                     "\n%s
" % line
                  """
""" else: """ Something is wrong with the input Address. [%s] Setting status to -1. """ % errormessage status = -1 if status: "Error in processing command[%s], exit code =%s " % (command, status) """Check for valid addresses. Embedded blanks are not allowed and must contain a dot "." and must not start with a "-".
""" processfooter(form, calctime_start, homeurl, __url__) return process()

Visitors to this web service will be naturally curious what IP address of their ISP is. We will add this feature SOON!

We just did! Take a look at the first screen shot. The second screenshot is the output page.




I just imported the get_sesson from qp.pub.common and made the call
remote_address= get_session.get_remote_address()

You can see the result in the first image above.

No comments:

Post a Comment