Network Byte Order
Network byte order is most-significant byte first.
Byte ordering at a host may differ.
socket.ntohl(x)
Convert 32-bit positive integers from network to host byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 4-byte swap operation.
socket.ntohs(x)
Convert 16-bit positive integers from network to host byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation.
socket.htonl(x)
Convert 32-bit positive integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 4-byte swap operation.
socket.htons(x)
Convert 16-bit positive integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation.
socket.inet_aton(ip_string)
Convert an IPv4 address from dotted-quad string format (for example, ‘123.45.67.89’) to 32-bit packed binary format, as a string four characters in length. This is useful when conversing with a program that uses the standard C library and needs objects of typestructin_addr
, which is the C type for the 32-bit packed binary this function returns.
inet_aton()
also accepts strings with less than three dots; see the Unix manual page_inet(3)_for details.
If the IPv4 address string passed to this function is invalid,socket.error
will be raised. Note that exactly what is valid depends on the underlying C implementation ofinet_aton()
inet_aton()
does not support IPv6, andinet_pton()
should be used instead for IPv4/v6 dual stack support.
socket.inet_ntoas(packed_ip)
Convert a 32-bit packed IPv4 address (a string four characters in length) to its standard dotted-quad string representation (for example, ‘123.45.67.89’). This is useful when conversing with a program that uses the standard C library and needs objects of typestructin_addr
, which is the C type for the 32-bit packed binary data this function takes as an argument.
If the string passed to this function is not exactly 4 bytes in length,socket.error
will be raised.inet_ntoa()
does not support IPv6, andinet_ntop()
should be used instead for IPv4/v6 dual stack support.
socket.inet_pton(address_family, ip_string)
Convert an IP address from its family-specific string format to a packed, binary format.inet_pton()
is useful when a library or network protocol calls for an object of typestructin_addr
(similar toinet_aton()
) orstructin6_addr
Supported values for_address_family_are currentlyAF_INET
andAF_INET6
. If the IP address string_ip_string_is invalid,socket.error
will be raised. Note that exactly what is valid depends on both the value of_address_family_and the underlying implementation ofinet_pton()
Availability: Unix (maybe not all platforms).
socket.inet_ntop(address_family, packed_ip)
Convert a packed IP address (a string of some number of characters) to its standard, family-specific string representation (for example,'7.10.0.5'
or'5aef:2b::8'
)inet_ntop()
is useful when a library or network protocol returns an object of typestructin_addr
(similar toinet_ntoa()
) orstructin6_addr
Supported values for_address_family_are currentlyAF_INET
andAF_INET6
. If the string_packed_ip_is not the correct length for the specified address family,ValueError
will be raised. Asocket.error
is raised for errors from the call toinet_ntop()
Availability: Unix (maybe not all platforms).