Thursday 10 September 2009

A new Django Podcast - Django Dose

With the demise of This Week in Django, there was a gap in the podcasting realm for Django news. Ruby and Rails have a couple of really decent podcasts, namely Ruby5 and Rails Envy.

Now there's Django Dose which, the dose guys say, it the spiritual successor to This Week in Django. I look forward to seeing how this one pans out, though it needs to get into iTunes soon; I searched for it there and had no luck.

Wednesday 9 September 2009

NTLM Authentication with Suds

We have a requirement to hit a SOAP web service running on a Windows box with IIS, and unfortunately this service is protected by NTLM authentication. While basic HTTP authentication is well supported and trivial to implement in Suds -- a Python SOAP client module -- NTLM is not.

There is a great blog entry describing how to solve this problem; however the post recommends placing the code within Suds itself, and I had to make a couple of minor modifications to get it to work with the with the latest Suds version (0.3.6).

Here are (very minor) the modifications to C. Bess's code:

from suds.transport.https import HttpAuthenticated
from suds.transport.http import HttpTransport
from ntlm import HTTPNtlmAuthHandler
import urllib2

class WindowsHttpAuthenticated(HttpAuthenticated):
"""
Provides Windows (NTLM) http authentication for suds.
@ivar pm: The password manager.
@ivar handler: The authentication handler.
@author: Christopher Bess (modified by Matt Sullivan)
"""
def __init__(self, **kwargs):
HttpTransport.__init__(self, **kwargs)
self.pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
self.handler =\
HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(self.pm)
self.urlopener = urllib2.build_opener(self.handler)
urllib2.install_opener(self.urlopener)

Using this authentication class is straightforward:

t = WindowsHttpAuthenticated(
username = 'DOMAIN\%s' %username,
password = password
)
url = 'http://www.somesite.com/service.asmx?wsdl'
return Client(url, transport = t)

Tuesday 8 September 2009

Testing HTTP using Telnet

While this is commonplace knowledge, I find myself googling for it every time I need to test HTTP connectivity and responses with telnet. Telnet provides a quick and dirty means to put back data over HTTP and to test the response headers.

Firstly, fire up telnet with the hostname and port for the HTTP connection:
telnet www.somesite.com 80
If it can connect to the server, then you will be presented with:

Trying www.somesite.com...
Connected to www.somesite.com.
Escape character is '^]'.

Now type the following to request index.html over HTTP:

GET /index.html HTTP/1.1
host: www.somesite.com

This will then return the header information, followed by the actual data.