Reputation: 71
I am having trouble getting an ipv6 address formatting into a packed binary string. After doing some digging, it appears that socket.inet_pton(...)
does not work in windows. While digging, I came across a suggestion to use ctypes and the InetPton function built into Ws2_32.dll
. Below is my simple script:
import ctypes
import socket
a = ctypes.WinDLL("ws2_32.dll")
in_addr_p = ctypes.create_string_buffer("200f::")
out_addr_p = ctypes.create_string_buffer(40)
a.inet_pton(socket.AF_INET6, in_addr_p, out_addr_p)
print "Input: {}".format(repr(in_addr_p.raw))
print "Output: {}".format(repr(out_addr_p.raw))
When I run it, I get the following:
Input: '200f::\x00'
Output: ' \x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00
I am stumped as to why it appears that "F" is the only character that makes it through the conversion process. '
Upvotes: 0
Views: 148
Reputation: 69032
See that space at the beginning of ' \x0f...'
? That's where the '20'
ended up, as \x20
is rendered as a space. So your code is working just fine...
If you're using python3.3 or newer, you should consider using the ipaddress module for such operations. There should also be backports for python2.
Upvotes: 4