On this page
socket.inet_pton
Prev: socket.inet_ntop | Up: socket | Next: socket.ntohl
Rel: socket.AddressFamily, socket.close, socket.create_connection, socket.create_server
Ref: docs.python.org — socket.inet_pton
Tags: #public #python3 #stdlib #socket
Import
import socket
from socket import inet_pton
Signature & Overview
socket.inet_pton(af, ip) -> packed IP address string
Convert an IP address from string format to a packed string suitable for use with low-level network functions.
Usage
import socket
assert socket.inet_pton(socket.AF_INET, "10.0.0.1") == b"\x0a\x00\x00\x01"
assert socket.inet_pton(socket.AF_INET6, "::1") == b"\x00" * 15 + b"\x01"
Gotchas
- Strict IPv4/IPv6 parser: Raises
OSErroron invalid strings; for high-level subnet math, use ipaddress.
REPL Inspect
from socket import inet_pton
help(inet_pton)
Linked from
Related notes
- tuts/python3/stdlib_module/socket/inet_ntop Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/ntohl Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/AddressFamily Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/close Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/create_connection Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/create_server Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/socket Linked here; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/ipaddress Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/socket links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/socket/dup shared tags: #socket, #stdlib