Prerequisites: Socket Programming in Python
Before going to the programming, let us discuss about ports. In this article, we will check the virtual ports of a server or websites, or localhost. Every port has a unique number. There are 65,535 ports available in a host starting from 0. We can assign the ports for any services.
Example 1: In this program, you can scan a number of ports in a certain range.
What you want to scan?: localhost Starting scan on host: 127.0.0.1 port 0 is closed port 1 is closed port 2 is closed port 3 is closed port 4 is closed Time taken 8.12 seconds
Note: you can change the range in the for loop for change the number of ports to be scanned. For scanning a website or a host it can take a certain time so be patient.
Example 2: If you want to scan a particular port then go for this solution.
What you want to scan?: localhost Starting scan on host: 127.0.0.1 Enter the port number to be scanned: 135 Port 135 is open
Note: Here we are scanning the localhost. You can scan any host or website. If you are getting any error then sockets is unable to connect the target or perhaps you did some mistake in your code.
Warnings: Without taking permission from the administration scanning ports of a server or a website can be considered as a crime. There are many free websites available for testing, you can use them.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.