SoFunction
Updated on 2024-12-16

A complete list of uses for the getservbyport and getservbyname functions in Python

In network programming in Python, the getservbyport() function and the getservbyname() function are two functions in the socket module, so you need to import the socket module when using these two functions.

1 getservbyname() function

The getservbyname() function works by specifying the name of a service to get the port number corresponding to that service.

RELATED 1 Services and Port Numbers. a host with an IP address can provide many services. these services are differentiated by port numbers. i.e. a port number corresponds to a service.

1.1 Grammar

The syntax of the getservbyname() function is shown below:

<code class="language-plaintext hljs">(servicename[, protocolname])</code>

where the parameter servicename is the name of the service to be looked up; the parameter protocolname is an optional parameter indicating the protocol name on which the service is based, which can be set to 'tcp' or 'udp'. The parameter can be set to 'tcp' or 'udp', if the parameter is not set, the service based on any protocol can be queried.

1.2 Relevant codes

1.2.1 Obtaining the port corresponding to a specified service

Use the code shown in Figure 1 to get the port corresponding to the specified service.

Figure 1 Getting the port corresponding to a specified service

The above code serves to get the port corresponding to the ftp service, i.e. port number 21.

1.2.2 Obtaining the port number of a specified service for a specified protocol

Use the code shown in Figure 2 to get the port corresponding to the specified service for the specified protocol.

Figure 2 Getting the port corresponding to a specified service for a specified protocol

The function of the above code is to get the port corresponding to the ftp service based on the tcp protocol, which is actually the same as the function of the code in Figure 1. If the protocol is set to upd, as shown in Figure 3, the code will report an error.

Figure 3 Getting the port corresponding to the udp-based ftp service

The role of the above code is to get the port corresponding to the ftp service based on the udp protocol, from the error message can be seen, did not find the ftp service based on the udp protocol, in fact, we use the ftp service are based on the tcp protocol.

2 getservbyport() function

The getservbyport() function works by specifying a port number to get the name of the service corresponding to that port number.

2.1 Grammar

The syntax of the getservbyport() function is shown below:

<code class="language-plaintext hljs">(port[, protocolname])</code>

where the parameter port is the specified port number; the parameter protocolname is an optional parameter indicating the name of the protocol on which the service is based, which can be set to 'tcp' or 'udp', and if the parameter is not set, the If this parameter is not set, services based on any protocol can be queried.

2.2 Related Codes

2.2.1 Getting the Service Name for a Specified Port

Use the code shown in Figure 4 to get the service name corresponding to the specified port.

Figure 4 Getting the service name corresponding to a specified port

The above code serves to get the name of the service corresponding to port 21, i.e. ftp service.

2.2.2 Obtaining the service name corresponding to a specified port number for a specified protocol

Use the code shown in Figure 5 to get the service name corresponding to the specified port number for the specified protocol.

Figure 5 Getting the service name corresponding to a specified port number for a specified protocol

The function of the above code is to get the service name corresponding to the port number 21 based on the tcp protocol, which is actually the same as the function of the code in Figure 1. If you set the protocol to upd, as shown in Figure 6, you will get another service name fsp. That is to say, both the ftp service and the fsp service use port 21, but the ftp service is based on the tcp protocol and the fsp service is based on udp.

Figure 6 Getting the service name corresponding to a specified port number for a specified protocol

Related 2, fsp is short for file server protocol which is a UDP based lightweight file transfer protocol.

2.2.3 Getting the service name corresponding to a port number in a specified range

You can use a for loop to get the service name corresponding to a port number in a specified range, as shown in Figure 7.

Figure 7 Getting the service name corresponding to a port number in a specified range

The function of the above code is to get the service name corresponding to the port number from 1-99, because some ports do not have a corresponding service name, at this time the getservbyport() function will report an error, so the use of exception handling when the port does not have a corresponding service name, the output of the corresponding information, as shown in Figure 8.

Figure 8 Service names corresponding to ports 1-99

to this article on the use of Python getservbyport and getservbyname function of the article is introduced to this, more related getservbyport and getservbyname function of the content of the use of getservbyport and getservbyname function, please search for my previous posts or continue to browse the following articles hope that you will support me in the future more!