Any DNS Modules?

Donn Cave donn at oz.net
Wed Oct 20 23:27:38 EDT 1999


Quoth amitp at Xenon.Stanford.EDU (Amit Patel):
|  Guido van Rossum  <guido at cnri.reston.va.us> wrote:
|| The DNS package is listed on the Python website (follow the Modules
|| link in the top bar).  Does nobody use that?  Searching python.org
|| for DNS would also have brought this up.
|| 
|
| I've looked at it but I never figured out how to use it.  :-( If I
| knew the DNS protocol (things like "MF", "WKS", "MX", "AXFR", ...) I
| would probably be able to figure out what the DNS package does, but I
| don't really know much about DNS -- all I want to do is turn hostnames
| into IP addresses and IP addresses into hostnames.  All this stuff
| about transfer functions and classes and types and building records is
| just too much for me.  (I currently use socket.* functions for that
| but I wanted something asynchronous.. so that's why I looked at the
| DNS package.)

Asynchronous, hm.  Well, is it OK to fork?  Try this, maybe ---

	Donn Cave, donn at oz.net
------------------------------
import pickle
import posix
import socket
import sys
import traceback

class Async:
	def __init__(self):
		pass
	def startquery(self, param, func):
		self.status = None
		self.rfd, self.wfd = posix.pipe()
		self.pid = posix.fork()
		if self.pid:
			posix.close(self.wfd)
		else:
			posix.close(self.rfd)
			self.perform(func, param)
	def checkquery(self):
		if self.status is None:
			p, s = posix.waitpid(self.pid, posix.WNOHANG)
			if p == self.pid:
				self.status = (s >> 8) & 0x7f
				if self.status == 0:
					s = posix.read(self.rfd, 8192)
					self.status, self.data = pickle.loads(s)
			else:
				return 0
		return 1
	def perform(self, func, param):
		try:
			data = apply(func, param)
			status = 0
		except:
			t, v, b = sys.exc_info()
			data = (t, v, traceback.format_exception(t, v, b))
			status = -1
		s = pickle.dumps((status, data))
		posix.write(self.wfd, s)
		sys.exit(0)
		posix._exit(101)

import time
a = Async()
a.startquery((sys.argv[1],), socket.gethostbyaddr)
while 1:
	x = a.checkquery()
	print x, repr(a.status)
	if x:
		break
	time.sleep(1.0)
if a.status < 0:
	et, ev, tb = a.data
	for ln in tb:
		sys.stderr.write(ln)
	raise et, ev
else:
	print repr(a.data)




More information about the Python-list mailing list