[Twisted-Python] Some beginner questions about "twisted.names.client" and ".tac" environment
Jesus Cea
jcea at argo.es
Sun Dec 18 10:16:06 EST 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jean-Paul Calderone wrote:
> You want to tie your application logic to a Service subclass. To start
> with, I'd try something like this, in dns3.py:
The very same error :-(.
My code for "dns3.tac":
=====
from twisted.application import service
application = service.Application("DNS test")
import sys
sys.path=["."]+sys.path
from dns3 import DomainResolver
DomainResolver().setServiceParent(application)
=====
My code for "dns3.py":
=====
class resolucion(object) :
def __init__(self,dominio) :
global myResolver
d = myResolver.lookupMailExchange(dominio,timeout=(1,))
d.addCallbacks(self._cbMailExchange, self._ebMailExchange)
self.dominio=dominio
def _cbMailExchange(self,results):
# Callback for MX query
global aun_pendientes
aun_pendientes-=1
if not aun_pendientes :
import time
print "OK",time.time()-t
#return
from twisted.internet import reactor
reactor.stop()
return
if not len(pendientes) :
return
resolucion(pendientes.pop())
from twisted.names import dns
for i in results[0] :
n=i.payload.name
tipo=i.payload.TYPE
if tipo==dns.MX :
return
p=i.payload.preference
print n,p,
for j in results[2] :
if n==j.name :
print j.payload.dottedQuad(),"(%d)" %j.ttl
break
else :
print "???"
elif tipo==dns.CNAME :
redirigidos.append((self.dominio,i.payload.name))
def _ebMailExchange(self,failure):
# Error callback for MX query
global aun_pendientes
aun_pendientes-=1
if not aun_pendientes :
import time
print "ERROR",time.time()-t
#return
from twisted.internet import reactor
reactor.stop()
return
if not len(pendientes) :
return
resolucion(pendientes.pop())
print "XXX",self.dominio
print 'Lookup failed:'
failure.printTraceback()
from twisted.application import service
class DomainResolver(service.Service):
def startService(self) :
global pendientes,redirigidos,aun_pendientes,t,myResolver
from twisted.names import client
myResolver=client.Resolver(servers=[('127.0.0.1', 53)])
import time
t=time.time()
pendientes=[]
redirigidos=[]
f=open("z1")
for i in f :
pendientes.append(i.split()[0])
f.close()
aun_pendientes=len(pendientes)
concurrencia=1
for i in pendientes[:concurrencia] :
resolucion(i)
pendientes=pendientes[concurrencia:]
=====
> This will delay the execution of your startup code until twistd is
> totally ready and the reactor is fully initialized.
Done, but not working :-(
[... DNS timeouts ...]
> Okay, it sounds like you know what you're doing here :)
I like to think so };-)
> I think the current caching resolver was a step in the wrong direction.
> A cache should probably *wrap* another resolver, not whatever weird
> thing it is doing now.
I agree. Perhaps a subclass of the network DNS resolver could be fine.
So we could have a "client" class and a "cachingClient" one.
In any case, current code simply doesn't cache anything, but eats CPU
cycles.
> If you write such a thing, it'd be great if you could submit it for
> inclusion :)
I'm not familiar enough with Twisted internal, yet, to try.
Nevertheless, I would suggest an optional parameter in the caching
resolver constructor, pointing to a callable object to resolve misses.
Sort of. Code changes would be minimal...
[... about differenciatiing DNS misses from RR misses...]
> I think flags like this are ugly hacks as well. I completely agree that
> subclass/override is not a great way to get this functionality, but I'd
> like to think of a cleaner way to offer the new feature while still
> maintaining backwards compatibility.
Keep me informed :-p
> Try the service class I used above. If you still see the exception, it
> may indicate a bug in Twisted's UDP support. If this case, could you
> attach the new version of the whole program? I'll take a closer look
> and see if I can nail down the exact cause.
Not working. Same exception. Code posted.
Thanks a lot for your help, Jean Paul. Greatly appreciated.
- --
Jesus Cea Avion _/_/ _/_/_/ _/_/_/
jcea at argo.es http://www.argo.es/~jcea/ _/_/ _/_/ _/_/ _/_/ _/_/
_/_/ _/_/ _/_/_/_/_/
PGP Key Available at KeyServ _/_/ _/_/ _/_/ _/_/ _/_/
"Things are not so easy" _/_/ _/_/ _/_/ _/_/ _/_/ _/_/
"My name is Dump, Core Dump" _/_/_/ _/_/_/ _/_/ _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibniz
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iQCVAwUBQ6V9Nplgi5GaxT1NAQKAOwP9H251Jjvd/kNPJf7R+S9faHN9YFcSBluL
YgBleuECDUG5WvJV2O+ZJwkHr05+qQ4qGzD13ydWnxdNCG1UMgWmIJ+TNfu2hOKN
L+rQmNKIUKuPFTOHA+tACYqJaE9rkkcJumR7FON81kNJdEShs52aNUlnc1Q6A9IB
iFyfaa9jcVI=
=V82G
-----END PGP SIGNATURE-----
More information about the Twisted-Python
mailing list