Getprotobyname() Function In Python

Function Name:

getprotobyname

 

Function Signature:

getprotobyname(NameOfTheProtocol)

 

Function Overview:

The function getprotobyname() takes a protocol string like TCP, UDP or ICMP and returns the associated constant for the protocol as defined by the socket module.

 

For example, for TCP the getprotobyname() returns a constant value of 6, and for UDP the constant returned by getprotobyname() is 17 and icmp the returned constant value is 7.

The returned value can be used as the value for 3rd parameter named proto of the socket function.

 

Parameters:

NameOfTheProtocol  - Name of the protocol for which the associated constant needs to be found.

 

The name of the protocol passed is not case sensitive.

 

Both upper case TCP and lower case tcp means the same protocol and the constant returned for both the strings is 6.

 

Example:

 

import socket

 

#TCP protocol

currentProtocol = "TCP"

protoConstant = socket.getprotobyname(currentProtocol)

print("Socket module constant associated with protocol {} is {}".format(currentProtocol,protoConstant))

 

#UDP protocol

currentProtocol = "UDP"

protoConstant = socket.getprotobyname(currentProtocol)

print("Socket module constant associated with protocol {} is {}".format(currentProtocol,protoConstant))

 

#ICMP protocol

currentProtocol = "ICMP"

protoConstant = socket.getprotobyname(currentProtocol)

print("Socket module constant associated with protocol {} is {}".format(currentProtocol,protoConstant))

 

 

Output:

The output of this function can be compared with the output of socket.getaddrinfo() as tuples returned by the getaddrinfo() function also contain the protocol constant.

Socket module constant associated with protocol TCP is 6

Socket module constant associated with protocol UDP is 17

Socket module constant associated with protocol ICMP is 1

 

 

 


Copyright 2023 © pythontic.com