Python Glossary Русский

all Python 3socketfunction

socket.getservbyport()

names and addresses

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

Syntax

socket.getservbyport(port, protocolname=None) -> str

Like getservbyname, it reads the local /etc/services table and has no idea what actually runs on the port. A port missing from the table raises OSError rather than returning None; a number outside 0–65535 raises OverflowError. For logging, catch OSError and print the number itself.

Examples

import socket
print(socket.getservbyport(443, 'tcp'))  # → https
try:
    socket.getservbyport(65000)
except OSError:
    print('нет в таблице')  # → нет в таблице
socket.getservbyport(70000)  # → OverflowError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy