Sunday, April 18, 2010

Django whois using the subprocess module

Last month I wrote a post about writing a whois client. This time I want to investigate If it's possible to leverage your operating system for those kind of tasks or if you really need to reinvent the wheel.

So why did I wrote my own crappy whois client in the first place? As you may already have noticed from my blog it's because I want to use the whois client from within Django (web application framework) hence the need to have a python module that can do whois queries.

Ok so the need is pretty clear but there should be a way to call the whois client of my operating system from a python module instead. This would allow me to write less code and I'll probably end up with a more robust whois client. So what possibilities does Python offer to spawn a process and communicate with it?

Since version 2.4 there is the subprocess module. In essence this is exactly what I need. So let's see how we can use it from within Django to provide a web interface to the whois native client.

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
import subprocess

def whois(domain):
    p = subprocess.Popen(['whois', domain], stdout=subprocess.PIPE)
    answer = p.commmunicate()
    return answer

class WhoisForm(forms.Form):
    domainname = forms.CharField(max_length=100)

if 'whois' in request.POST:
    whois_form = WhoisForm(request.POST, prefix='whois')
    if whois_form.is_valid():
        domainname = whois_form.cleaned_data['domainname']
        answer = whois(domainname)
else:
    whois_form = WhoisForm( prefix='whois')

return render_to_response('index.html', {'whois_form': whois_form,
                                         'answer': answer,})

What happens in the code above is that we spawn the whois subprocess and pipe its output. This enables us to communicate with it and retrieve it's standard output in a variable. We can then pass along the output to the template for the user to view in his browser. Quick and easy!

Ok, so at this point I should be a happy man! I did reach my initial goal of not reinventing the wheel. However  I'm really not sure this is the way to go. Next time we will use the timeit module to see how this one compares to the python whois client regarding performance.

3 comments:

  1. I know this is an old post, but how did you get the whois output to format properly on web output? Right now for me I'm getting it with parens and the \n characters. Using pre tags in HTML dosen't help unfortunately.

    ReplyDelete
  2. The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.
    whois reverse lookup

    ReplyDelete
  3. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai

    ReplyDelete