Inet_ntop() - Convert An IP Address In Binary Format To A String Using Python

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:

# 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:

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

IP address in human readable string format: 192.0.2.255


Copyright 2023 © pythontic.com