[Twisted-Python] Twisted HTTP client supporting failover for multiple A records?

Itamar Turner-Trauring itamar at itamarst.org
Thu Jul 15 08:06:00 EDT 2010

On Thu, 2010-07-15 at 10:46 +0100, Reza Lotun wrote:

> As for connecting to hosts that resolve to multiple A records - I
> presume as a means of load balancing via DNS round robin - I'm not
> quite sure this is natively supported in Twisted. I believe since all
> TCP connections are mediated via connectTCP hostnames are ultimately
> resolved via socket.gethostbyname. I think you really want the support
> provided by socket.gethostbyname_ex
> (http://docs.python.org/library/socket.html#socket.gethostbyname_ex).
> It's a good question though. I'm sure a core dev will come along and
> give a proper answer soon ;-)

Gar. I should read better. Twisted uses a threadpool of gethostname by
default, but you can plug in your own resolver (e.g. you can use


The question is whether the client code re-resolves on each re-connect,
and whether the current lookup interface is sufficient for this use

Alas, I'm pretty sure the answer is no.

You could however always just do the DNS lookup yourself, passing
resulting correct IP to connectTCP, just make sure you don't block (e.g.
by using deferToThread to call gethostbyname_ex).

More information about the Twisted-Python mailing list