[Twisted-Python] twisted listening on UDP port, why?

Jonathan Vanasco twisted-python at 2xlp.com
Fri Apr 22 20:36:41 MDT 2016


Thanks for all this.  

A few quick notes:

On Apr 20, 2016, at 9:15 PM, Glyph wrote:

> This isn't so much a feature of Python as it is a feature of the BSD sockets API.  Sending traffic through a socket, whether it's TCP or UDP, has to bind a client port.  Given the nature of UDP, binding on all interfaces is the expectation unless you specify.
> 
> I didn't have time to test a simple C program before sending this message, but https://github.com/python/cpython/blob/master/Modules/socketmodule.c only calls "bind()" from sock_bind, not from send(), nor does https://github.com/python/cpython/blob/master/Lib/socket.py engage in any such shenanigans.

The 'feature' of Python is a few things:

From what I could tell, the actual communication and binding happens somewhere in the c module.  

if you just construct socket:

    sock = socket.socket(family, socket.SOCK_DGRAM)

it will defer the bind on 0.0.0.0 until the first time data is emitted:

    sock.sendto(data.encode('ascii'), addr)

That's one of the things that drove me crazy (and the reason why I'm posting more than a simple "thank you", in case someone else gets stumped in the future).  
There's no call to anything with a "bind" involved in any python code.  it just happens behind the scenes.

> There's also a Twisted version :) https://pypi.python.org/pypi/txStatsD
> 
> txStatsD contains both server and client, so maybe you want to use that client if you want better control over the UDP port.

yeah, I'm gonna do that ;)

> That range is the ephemeral client port range <https://en.wikipedia.org/wiki/Ephemeral_port> so that's what would be expected of an implicitly-bound socket.

THANK YOU.  I could not remember that name.  I knew about the ephemeral port range, but have not seen that term in 10+ years.  THANK YOU!!!!


-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20160422/9c424490/attachment-0002.html>


More information about the Twisted-Python mailing list