Python Glossary Русский

all Python 3socketfunction

socket.getprotobyname()

names and addresses

Translates a protocol name ('tcp', 'udp', 'icmp') into its number for the third argument of socket().

Syntax

socket.getprotobyname(protocolname) -> int

It is needed almost only for raw sockets (SOCK_RAW): with proto=0 the protocol for TCP and UDP is chosen automatically. Numbers come from the local file /etc/protocols, not from the network; minimal Docker images lack that file and the call fails with OSError. The ready-made constants socket.IPPROTO_TCP and IPPROTO_UDP are more reliable.

Examples

import socket
print(socket.getprotobyname('tcp'))  # → 6
print(socket.getprotobyname('udp') == socket.IPPROTO_UDP)  # → True
socket.getprotobyname('no-such-proto')  # → OSError: protocol not found

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy