win-inet-pton

raw JSON →
1.1.0 verified Fri May 01 auth: no python

Provides native inet_pton and inet_ntop implementations for Windows using ctypes. Version 1.1.0 is current; maintenance mode with rare updates.

pip install win-inet-pton
error ImportError: No module named win_inet_pton
cause Library not installed via pip.
fix
Run pip install win-inet-pton (note hyphen in package name).
error AttributeError: module 'socket' has no attribute 'inet_pton'
cause Import succeeded but monkey-patching was not performed.
fix
After importing, assign: socket.inet_pton = inet_pton (and optionally inet_ntop).
error OSError: [WinError 10042] An address incompatible with the requested protocol was used
cause Using `socket.inet_pton` with an incorrect address family or invalid address string.
fix
Ensure you pass the correct family (socket.AF_INET or socket.AF_INET6) and a valid IP string.
gotcha This library is Windows-only. Importing it on non-Windows systems will raise an ImportError unless you guard it. Always use the check `if sys.platform == 'win32':` before importing.
fix Wrap import in a platform check.
gotcha The library monkey-patches the built-in socket module. If you assign the functions via `socket.inet_pton = inet_pton`, ensure you do it only once to avoid double-patching. Doing it repeatedly is harmless but confusing.
fix Apply the patch in a single location, e.g., at the start of your application.
deprecated Python 3.4+ on Windows includes official `inet_pton` support in the socket module. This library is only needed for Python 2.7 or earlier Python 3 versions. For modern Python, consider dropping it.
fix For Python 3.4+, remove the dependency; the functions are built-in.

Monkey-patches socket module on Windows so inet_pton and inet_ntop work the same as on Unix.

import socket
from win_inet_pton import inet_pton, inet_ntop

# Monkey-patch socket module to add missing functions on Windows
socket.inet_pton = inet_pton
socket.inet_ntop = inet_ntop

# Now you can use inet_pton and inet_ntop directly via socket
print(socket.inet_pton(socket.AF_INET, '8.8.8.8'))