This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Kurt.Rose
Recipients Kurt.Rose
Date 2015-05-12.18:50:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1431456658.59.0.16323204969.issue24169@psf.upfronthosting.co.za>
In-reply-to
Content
This appears to affect all versions of Python.  In a behavior inherited from C, TCP ports that are > 2 bytes are silently truncated.

Here is a simple reproduction:

>>> socket.create_connection( ('google.com', 2**16 + 80) )
<socket.socket object, fd=408, family=2, type=1, proto=0>

Needs more investigation, but one likely place to make the fix is here:
https://hg.python.org/cpython/file/9d2c4d887c19/Modules/socketmodule.c#l1535

        if (!PyArg_ParseTuple(args, "O&i:getsockaddrarg",
                              idna_converter, &host, &port))

Instead of parsing port with i, use H.  This is a deep change to the behavior, but I think it is very unlikely that users intentionally mean to pass a TCP port > 2**16.  More likely, this is silently swallowing errors. 

There no indication that the passed port parameter is not being used for the actual TCP communication (which is physically impossible because TCP only has a 2 byte port field).

In fact, the socket will continue to "lie" to the user and obfuscate the actual port being used if getsockname() is called:

>>> socket.create_connection( ('google.com', 2**16 + 80) ).getsockname()
('10.225.89.86', 54899)
History
Date User Action Args
2015-05-12 18:50:58Kurt.Rosesetrecipients: + Kurt.Rose
2015-05-12 18:50:58Kurt.Rosesetmessageid: <1431456658.59.0.16323204969.issue24169@psf.upfronthosting.co.za>
2015-05-12 18:50:58Kurt.Roselinkissue24169 messages
2015-05-12 18:50:58Kurt.Rosecreate