Python-scapy: how to translate port numbers to service names


A TCP layer in Scapy contains source port:

>>> a[TCP].sport

Is there a simple way to convert port number to service name? I've seen Scapy has TCP_SERVICES and UDP_SERVICES to translate port number, but

print TCP_SERVICES[80] # fails
print TCP_SERVICES['80'] # fails
print TCP_SERVICES.__getitem__(80) # fails
print TCP_SERVICES['www'] # works, but it's not what i need

Someone know how can I map ports to services?

Thank you in advance

Best Solution

Python's socket module will do that:

>>> import socket
>>> socket.getservbyport(80)
>>> socket.getservbyport(21)
>>> socket.getservbyport(53, 'udp')