inet_ntop() - Convert an IP address in binary format to a string using Python

The inet_ntop() function from socket module of Python converts an IP address in packed binary format to string format. The Python example program converts the IP address from a binary string b"xc0x00x02xff" to a string "192.0.2.255" which is in human readable format.

socket Updated 24 Jun 2019 1 min read

Overview

  • The Python function inet_pton() from the socket module converts an IP address in packed binary format to string format.

Example – Convert an IPv4 IP address from binary format to string format

python
# Example Python program to convert an IPv4 address

# from packed binary format to string

import socket

ipAddress_bin   = b"\xc0\x00\x02\xff";

ipAddress_str   = socket.inet_ntop(socket.AF_INET, ipAddress_bin);

print("IP address in binary format:%s"%ipAddress_bin);

print("IP address in human readable string format: %s"%(ipAddress_str));

Output

Output
IP address in binary format:b'\xc0\x00\x02\xff'

IP address in human readable string format: 192.0.2.255