Python Glossary Русский

all Python 3socketfunction

socket.getservbyname()

names and addresses

Returns the standard port number for a service name: 'http' gives 80, 'ssh' gives 22.

Syntax

socket.getservbyname(servicename, protocolname=None) -> int

This does not query any server: the table comes from the local /etc/services (or the Windows equivalent), so it knows nothing about the port a service actually runs on. Minimal containers lack the file and the call fails with OSError. The second argument is 'tcp' or 'udp'; without it any protocol matches.

Examples

import socket
print(socket.getservbyname('http', 'tcp'))  # → 80
print(socket.getservbyname('https'))  # → 443
socket.getservbyname('no-such-service')  # → OSError: service/proto not found

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy