[Twisted-Python] async hostname resolution

Jp Calderone exarkun at meson.dyndns.org
Fri Nov 22 20:55:04 MST 2002


  Diff for twisted/internet/base.py's resolve() function to make
it perform lookups asynchronously using the new stuff in
twisted/names/client.py.

  I removed the type argument, since it complicates implementation for other
reactors, isn't really useful in 99% of cases, and never worked when set to
anything other than A (1) anyway.  Maybe it should be deprecated first for a
while, but I doubt anyone's using it anyway so it may not matter.

  Jp

-- 
<Greatnuts> someone gave me bad advice this morning
<Greatnuts> told me to use twm
<Greatnuts> had to reformat
<Greatnuts> just to get it off
--
 10:00pm up 21 days, 8:53, 6 users, load average: 0.00, 0.00, 0.00
-------------- next part --------------
Index: base.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/base.py,v
retrieving revision 1.27
diff -u -r1.27 base.py
--- base.py	30 Oct 2002 05:56:34 -0000	1.27
+++ base.py	23 Nov 2002 03:42:03 -0000
@@ -21,8 +21,6 @@
 Maintainer: U{Itamar Shtull-Trauring<mailto:twisted at itamarst.org>}
 """
 
-import socket # needed only for sync-dns
-
 import warnings
 from time import time
 from bisect import insort
@@ -135,22 +133,13 @@
     def removeAll(self):
         raise NotImplementedError
 
-    def resolve(self, name, type=1, timeout=10):
+    def resolve(self, name, timeout=10):
         """Return a Deferred that will resolve a hostname.
         """
-        # XXX TODO: alternative resolver implementations
-        from twisted.internet.defer import Deferred
-        deferred = Deferred()
-        if type == 1:
-            try:
-                address = socket.gethostbyname(name)
-            except socket.error:
-                deferred.errback(failure.Failure(error.DNSLookupError("address not found")))
-            else:
-                deferred.callback(address)
-        else:
-            deferred.errback(failure.Failure(ValueError("type not supported")))
-        return deferred
+        from twisted.names import client
+        from twisted.protocols import dns
+        return client.lookupAddress(name).addCallback(lambda m: m[0])
+
 
     # Installation.
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20021122/27b147ea/attachment.sig>


More information about the Twisted-Python mailing list