In this section, we'll review how we can implement port-scanning with sockets and how to manage exceptions when we are working with sockets.
Practical use cases for sockets
Port scanner with sockets
Sockets are the fundamental building block for network communications and in an easy way we can check whether a specific port is open, closed, or filtered by calling the connect_ex method.
For example, we could have a function that accepts by parameters an IP and a list of ports and return for each port whether it is open or closed.
In this example, we need to import the socket and sys modules. If we execute the function from our main program, we see how it checks each of the ports and returns whether it is open or closed...