Monday, April 21, 2025

Get Service by Port - Python getservbyport()

Learn More:

Free Python Programming Course
Python Examples
Python How To
Python Modules 

YouTube WebDevPro Tutorials 

import socket, os

#port_list = [21, 22, 23, 139, 137, 80, 443, 445]

while True:
    port = input('Please enter port: ')

    if port == 'q':
        print('Done')
        break
    
    else:
        os.system('cls')
        print('-' * 50)
        print('Result: ' + socket.getservbyport(int(port)))

This Python code performs the following tasks:

  1. Imports the 'socket' and 'os' modules using the 'import' keyword. The 'socket' module provides a way to communicate over a network, while the 'os' module provides a way to interact with the operating system.

  2. Defines a 'while' loop that runs indefinitely.

  3. Within the loop, prompts the user to input a port number using the 'input()' function.

  4. Checks if the user input is equal to the letter 'q'. If so, it prints a message and exits the loop using the 'break' keyword.

  5. If the user input is not equal to 'q', clears the console using the 'os.system()' method, then prints a separator string and the input value to the console using the 'print()' function.

  6. Calls the 'socket.getservbyport()' method, passing the integer value of the input as an argument. This method returns the name of the service associated with the given port number.

  7. Concatenates the returned service name with a message string, and then prints the resulting string to the console using the 'print()' function.

Let's break down each line of code in more detail:

import socket, os

This line imports the 'socket' and 'os' modules into the script, which provide access to network communication and operating system functions respectively.

while True:

This line starts an infinite 'while' loop that will keep running until it is explicitly exited.

port = input('Please enter port: ')

This line prompts the user to enter a port number via the console using the 'input()' function, and stores the resulting value in the variable 'port'.

if port == 'q':
    print('Done')
    break

This 'if' statement checks if the user entered the letter 'q'. If so, it prints a message to the console using the 'print()' function and exits the loop using the 'break' keyword.

else:
    os.system('cls')
    print('-' * 50)
    print('Result: ' + socket.getservbyport(int(port)))

If the user did not enter 'q', then this block of code is executed. The 'os.system()' method is called with the argument 'cls', which clears the console screen. The 'print()' function is then used to print a separator string to the console. Finally, the 'socket.getservbyport()' method is called with the integer value of the 'port' variable as an argument, and the returned service name is concatenated with a message string and printed to the console using the 'print()' function.

This Python code prompts the user to enter a port number via the console, then looks up the name of the service associated with that port using the 'socket.getservbyport()' method. The program will continue to prompt the user for input until the letter 'q' is entered, at which point it will exit.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...