Created
March 11, 2014 16:49
-
-
Save Jc2k/9489914 to your computer and use it in GitHub Desktop.
inet_pton monkey patch for windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| if not hasattr(socket, "inet_pton"): # pragma: no cover | |
| import ctypes | |
| WSAStringToAddressA = ctypes.windll.ws2_32.WSAStringToAddressA | |
| class sockaddr(ctypes.Structure): | |
| _fields_ = [ | |
| ("sa_family", ctypes.c_short), | |
| ("__pad1", ctypes.c_ushort), | |
| ("ipv4_addr", ctypes.c_byte * 4), | |
| ("ipv6_addr", ctypes.c_byte * 16), | |
| ("__pad2", ctypes.c_ulong) | |
| ] | |
| def inet_pton(address_family, ip_string): | |
| addr = sockaddr() | |
| addr.sa_family = address_family | |
| addr_size = ctypes.c_int(ctypes.sizeof(addr)) | |
| if WSAStringToAddressA(ip_string, address_family, None, ctypes.byref(addr), ctypes.byref(addr_size)) != 0: | |
| raise socket.error(ctypes.FormatError()) | |
| if address_family == socket.AF_INET: | |
| return ctypes.string_at(addr.ipv4_addr, 4) | |
| elif address_family == socket.AF_INET6: | |
| return ctypes.string_at(addr.ipv6_addr, 16) | |
| else: | |
| raise socket.error('unknown address family') | |
| setattr(socket, "inet_pton", inet_pton) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment