开发者

Not getting the correct result back from inet_aton/struct.unpack

Using python开发者_运维技巧 2.6.5 on Windows XP, it seems I'm getting the wrong result when using the following code:

import struct
import socket
struct.unpack('L', socket.inet_aton('192.168.1.1'))[0]

This returns 16885952 while to my knowledge it should return 3232235777. Am I doing something wrong here? How do I fix this?


You need to specify the endianess. Its interpreting the number as litle-endian, however inet_aton returns the number as big-endian.

struct.unpack('>L', socket.inet_aton('192.168.1.1'))[0]

This should work fine, and return the number you expected.

See the python documentation on "struct" for reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜