March 2019
Beginner
490 pages
12h 40m
English
The socket module provides the socket.getservbyport(port[, protocol_name]) method, which allows us to get the port name from the port number. For example:
>>> import socket>>> socket.getservbyport(80)'http'>>> socket.getservbyport(23)'telnet'
We can also get information about the service name at the application level if we pass the protocol name as a second parameter.
You can find the following code in the socket_finding_service_name.py file:
#!/usr/bin/env python3import socketdef find_service_name(): protocolname = 'tcp' for port in [80, 25]: print ("Port: %s => service name: %s" %(port, socket.getservbyport(port, protocolname))) print ("Port: %s => service name: %s" %(53, socket.getservbyport(53, ...Read now
Unlock full access