Python error with socket. Ntohl()
I'm having an issue with the socket.ntohl()
function, on some hosts. It is repeatable on all similar hosts; 32bit machines with Python 开发者_开发知识库2.4.2.
>>> socket.ntohl(16777215)
-256
However the reverse seems to work fine -
>>> socket.htonl(4294967040)
16777215
Reading the docs, It doesn't mention any limitations or warnings. Is this a bug in the Novell version of this old package? They are all Suse 9 machines :(
In older 32-bit Python versions, int
was limited to a signed 32-bit number. 16777215 = 0x00FFFFFF and -256 in 32-bit 2s complement is 0xFFFFFF00.
It works correctly in Python 2.7 by upgrading the value to a long:
>>> socket.ntohl(16777215)
4294967040L
>>> hex(4294967040)
'0xffffff00L'
Edit:
Python 2.4 was the first version to unify int
and long
so what you see might be considered a bug that has been fixed by 2.7.
Looks like this issue fixed it.
精彩评论