there is a dns modul for Python (I don't know is it part of standard Python library or not), on most Linux distribution you can find it, eg. in Debian it's called python-dnspython.
It can handle different nameserver, than OS knows - here is a sample code:
r = dns.resolver.Resolver()
r.namerservers = ['127.0.0.1']
# or any other IP, in my case I'm using PDNS, which have two
# parts: a recursor and a resolver; recursor allows requests only
# on localhost
mxservers = r.query("python.org", 'MX').response