the protocol like tcp and udp is all represented by a number.
import socket
socket.getprotocobyname('tcp')
the above code will return 6.
How I can get the protocol name if I know the protocol number?
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:
import socket
prefix = "IPPROTO_"
table = {num:name[len(prefix):]
for name,num in vars(socket).items()
if name.startswith(prefix)}
assert table[6] == 'TCP'
assert table[0x11] == 'UDP'
print(len(table)) # in python 3.10.0 this has 30 entries
from pprint import pprint
pprint(table) # if you want to see what is available to you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With