On this page
socket.htonl
Prev: socket.herror | Up: socket | Next: socket.htons
Rel: socket.AddressFamily, socket.close, socket.create_connection, socket.create_server
Ref: docs.python.org — socket.htonl
Tags: #public #python3 #stdlib #socket
Import
import socket
from socket import htonl
Signature & Overview
socket.htonl(integer, /)
Convert a 32-bit unsigned integer from host to network byte order.
Usage
import socket
# Host-to-network 32-bit unsigned integer byte-order conversion
net = socket.htonl(0x01020304)
assert socket.ntohl(net) == 0x01020304
Gotchas
- Prefer
struct.pack("!I", val)orval.to_bytes(4, "big")when serializing multi-field network packets.
REPL Inspect
from socket import htonl
help(htonl)
Linked from
Related notes
- tuts/python3/stdlib_module/socket/herror Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/htons 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/socket links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/socket/dup shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/error shared tags: #socket, #stdlib