Opened 9 years ago

Closed 9 years ago

#3522 defect closed duplicate (duplicate)

getHostByName bug: "Please only pass IPs to write(), not hostnames"

Reported by: palo Owned by:
Priority: normal Milestone:
Component: names Keywords: dns hostname names ip getHostByName block blocking udp
Cc: Branch:
Author:

Description

Hello,

it seems there is a bug in t.n.c.Resolver.getHostByName():

from twisted.internet import reactor
from twisted.names.client import Resolver
from twisted.internet.defer import DeferredList


DOMAIN_NAMES = ('ebay.de', 'google.de', 'foo.de', 'abc.de')


def cb(result):
    print 'in cb()'
    print 'result:', result

def gotError(failure):
    print 'in gotError'
    failure.printTraceback()

def finished(result):
    reactor.stop()

r = Resolver(servers=[('81.91.164.5', 53)])
dlist = []

for domain in DOMAIN_NAMES:
    d = r.getHostByName(domain)
    d.addCallbacks(cb, gotError)
    dlist.append(d)

dl = DeferredList(dlist)
dl.addBoth(finished)

reactor.run()

results in:

/usr/lib/python2.5/site-packages/Twisted-8.1.0-py2.5-linux-i686.egg/twisted/names/dns.py:1479: DeprecationWarning: Please only pass IPs to write(), not hostnames
  self.transport.write(message.toStr(), address)
in cb()
result: 212.227.4.54
in cb()
result: 194.49.7.145
in cb()
result: 216.239.59.104
in cb()
result: 66.211.160.87

If I put a print statement in dns.py:1479, printing the address, I get this:

('81.91.164.5', 53)
('81.91.164.5', 53)
('81.91.164.5', 53)
('81.91.164.5', 53)
('smf-dns2.ebaydns.com', 53)
/usr/lib/python2.5/site-packages/Twisted-8.1.0-py2.5-linux-i686.egg/twisted/names/dns.py:1479: DeprecationWarning: Please only pass IPs to write(), not hostnames
  self.transport.write(message.toStr(), address)
('ns2.google.com', 53)
('ns4.schlund.de', 53)
('213.178.67.30', 53)

Why is there only one warning? I think it should be 3, because there're 3 domain names.

There is even already a ticket and a patch: Ticket #2850
(but unfortunately the patch doesn't seems to work).

From the ticket:

"In addition, self.socket.sendto(datagram, addr) will call socket.gethostbyname(addr), but this call is block."

Are the dns request of the script above blocking? How can I test this?

Btw, can someone tell me where I can find the source code of reactor.connectTCP?
(I want to look how connectTCP resolves domain names).

help(twisted.internet.reactor.connectTCP) says:

"look at twisted.internet.interfaces.IReactorTCP.connectTCP"

But there is *nothing* but a doc string.

Thank you very much.

greets,
palo

PS: Sorry for my bad english

Change History (4)

comment:1 Changed 9 years ago by Glyph

Resolution: invalid
Status: newclosed

This is just how the Python warnings system works. Normally, if the same warning is emitted multiple times, it will only be displayed the first time. You can ask Python to emit warnings multiple times with the -Walways command-line switch, or by calling warnings.filterwarnings("always").

comment:2 Changed 9 years ago by palo

Resolution: invalid
Status: closedreopened

Thanks for the fast reply, but this bug report wasn't about the number of occurrences of the warnings (that was just a side note). It was about *the reason* of the warnings. I think this warnings aren't there without a reason (otherwise they should be disabled).

I think instead of throwing this warning twisted should try to resolve the host to a IP, wouldn't that be better? In the other bug report wangminghua wrote, otherwise self.socket.sendto(datagram, addr) will call socket.gethostbyname(addr) and this would block the whole application. Is that true?

comment:3 Changed 9 years ago by Glyph

Resolution: duplicate
Status: reopenedclosed

Aah, sorry for closing as invalid, I misunderstood your complaint. This is entirely a valid bug. Unfortunately I am still going to close it :), since as you say - there's already a ticket. Please feel free to attach more information to #2850.

Also, if I've misunderstood yet again and this is reporting a different problem than #2850, go ahead and reopen with an explanation of the difference.

comment:4 Changed 7 years ago by <automation>

Owner: Jean-Paul Calderone deleted
Note: See TracTickets for help on using tickets.