the protocol like tcp and udp is all represented by a number.
JavaScript
x
3
1
import socket
2
socket.getprotocobyname('tcp')
3
the above code will return 6.
How I can get the protocol name if I know the protocol number?
Advertisement
Answer
I’m going to say there is almost definitely a better way then this, but all the protocol names (and values) are stored as constants prefixed by "IPPROTO_"
so you can create a lookup table by iterating over the values in the module:
JavaScript
1
12
12
1
import socket
2
prefix = "IPPROTO_"
3
table = {num:name[len(prefix):]
4
for name,num in vars(socket).items()
5
if name.startswith(prefix)}
6
7
assert table[6] == 'TCP'
8
assert table[0x11] == 'UDP'
9
print(len(table)) # in python 3.10.0 this has 30 entries
10
from pprint import pprint
11
pprint(table) # if you want to see what is available to you
12