On this page
socket.htons
Prev: socket.htonl | Up: socket | Next: socket.if_indextoname
Rel: socket.AddressFamily, socket.close, socket.create_connection, socket.create_server
Ref: docs.python.org — socket.htons
Tags: #public #python3 #stdlib #socket
Import
import socket
from socket import htons
Signature & Overview
socket.htons(integer, /)
Convert a 16-bit unsigned integer from host to network byte order.
Usage
import socket
# Host-to-network 16-bit unsigned integer byte-order conversion
net = socket.htons(8080)
assert socket.ntohs(net) == 8080
Gotchas
- 16-bit unsigned only (
0..65535): For binary wire protocols,struct.pack("!H", port)is usually clearer.
REPL Inspect
from socket import htons
help(htons)
Linked from
Related notes
- tuts/python3/stdlib_module/socket/htonl Linked here; links to this page; shared tags: #socket, #stdlib
- tuts/python3/stdlib_module/socket/if_indextoname 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