Python Glossary Русский

all Python 3socketfunction

socket.inet_ntoa()

address and byte-order conversion

Converts 4 bytes of an IPv4 address in network order into a string like '192.168.0.1'.

Syntax

socket.inet_ntoa(packed_ip) -> str

It takes exactly 4 bytes: any other length raises OSError instead of truncating. A typical source is a raw packet header or struct.unpack('4s', …). IPv6 needs inet_ntop(AF_INET6, …); for address arithmetic ipaddress.IPv4Address(bytes) is handier and accepts the same 4 bytes.

Examples

import socket
print(socket.inet_ntoa(b'\x7f\x00\x00\x01'))  # → 127.0.0.1
print(socket.inet_ntoa(socket.inet_aton('10.0.0.255')))  # → 10.0.0.255
socket.inet_ntoa(b'abc')  # → OSError: packed IP wrong length for inet_ntoa

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy