Tuesday 20 October 2009

MacPorts Reference

I use MacPorts to install a lot of development software: latest Python versions, MySQL, MongoDB, etc. And I can never recall the various port commands, so I'm listing the ones I use here:

Update MacPorts: sudo port selfupdate

Search for a port: port search port_name

Install a new port: sudo port install port_name

List a port's variants: sudo port variants port_name

Install a new variant port: sudo port install port_name +variant_name

List installed ports: port installed

List outdated ports: port outdated

Update outdated ports: sudo port install outdated

Clean up temp files: sudo port clean --all installed

Delete old install versions: sudo port -f uninstall inactive

They're the ones I use the most. Anyone suggest others that are handy?

Thursday 8 October 2009

Python through Osmosis

A short note: Python Osmosis is a great video podcast for those who want to learn, or brush up on aspects of, Python. Get it from the website, or subscribe through iTunes.

Saturday 3 October 2009

An Alternative to ElementTree -- lxml

I use ElementTree as my XML library of choice in Python, but sometimes it is lacking in what I need it to do. I have always found its support for namespaces to be awkward to use, and recently I needed to validate generated XML against a collection of XML schemas, but ElementTree has no support for this.

So I had a hunt around and discovered lxml, a Python XML library that -- not only appears to actively developed -- provides good compatibility with ElementTree while layering on additional functionality.

The only downside to lxml is that it requires both libxml2 and libxslt to be installed, though if you install from the binary egg, it includes these libraries, making it very straightforward to run on Windows. On the Mac, you might need to upgrade the libraries, which can be a chore, though using MacPorts makes life easier.

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.