[Twisted-Python] Specifying the local port for a twisted.names DNS query or resolver

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Jun 15 07:56:34 EDT 2011


On 05:52 am, anton at iki.fi wrote:
>On Tue, Jun 14, 2011 at 9:01 PM,  <exarkun at twistedmatrix.com> wrote:
>>We'd be happy to have such a feature officially supported.  Would you
>>like to work on a patch that adds a tested, documented hook for
>>selecting the source port?
>
>Perhaps I could work on such a patch. I would be thankful for any tips
>on where to start and what would be a good interface.
>
>I looked at the code again, and I think the source port is selected by
>calling dns.randomSource() deep in
>twisted.names.client.Resolver._connectedProtocol(). This could perhaps
>be broken out to a separate method of client.Resolver. It would
>probably make sense to at the same time let the local IP address
>(interface) be specified in addition to the port (as with the
>bindAddress parameter). So perhaps Resolver._connectedProtocol() could
>call a new method client.Resolver.getUDPBindAddress(), with the
>default implementation returning ('', dns.getRandomSource()). Then a
>user could subclass Resolver and override the getUDPBindAddress method
>(or monkey patch an existing Resolver).
>
>Currently Resolver._connectedProtocol() wraps the call to
>dns.randomSource() and listenUDP in a "while True" loop, catching
>CannotListenError so that if the randomly selected port is in use, a
>new port will be selected by random. If we let the user change the
>port selection logic, it might be good to safeguard against infinitely
>looping if e.g., the user decides to always return the same port.
>Perhaps re-raise the CannotListenError exception on the N-th loop.
>
>Am I on the right track?

Yep, sounds like a great plan.

Jean-Paul



More information about the Twisted-Python mailing list