GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — How to Make a Port Scanner in Python - The Python Code Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting now! Port scanning is a scanning method for determining which ports on a network device are open, whether it's a server, a router, or a regular machine. A port scanner is just a script or a program that is designed to probe a host for open ports. In this tutorial, you will be able to make your own port scanner in Python using the For instance, when you loaded this web page, you made a connection to this website on port 80. Similarly, this script will try to connect to a host but on multiple ports. These kinds of tools are useful for hackers and penetration testers, so don't use this tool on a host that you don't have permission to test! Table of content: GET: Build 35+ Ethical Hacking Scripts & Tools with Python EBook Optionally, you need to install
Simple Port ScannerFirst, let's start by making a simple port scanner. Let's import the
Note: The socket module provides us with socket operations, functions for network-related tasks, etc. They are widely used on the Internet, as they are behind any connection to any network. Any network communication goes through a socket. More details are in the official Python documentation. We will use Let's define the function that is responsible for determining whether a port is open:
Now let's use the above function and iterate over a range of ports:
The above code will scan ports ranging from 1 all the way to 1024, you can change the range to 65535 if you want, but that will take longer to finish. When you try to run it, you'll immediately notice that the script is quite slow. Well, we can get away with that if we set a timeout of 200 milliseconds or so (using Read also: How to Use Shodan API in Python. Fast (Threaded) Port ScannerNow let's take our simple port scanner to a higher level. In this section, we'll write a threaded port scanner that can scan 200 or more ports simultaneously. The below code is actually the same function we saw previously, which is responsible for scanning a single port. Since we're using threads, we need to use a lock so only one thread can print at a time. Otherwise, the output will be messed up, and we won't read anything useful:
So this time, the function doesn't return anything; we just want to print whether the port is open (feel free to change it, though). We used
Get Our Ethical Hacking with Python EBookMaster Ethical Hacking with Python by building 35+ Tools from scratch. Get your copy now! Download EBookThe job of the Note the Finally, let's make a simple argument parser using
Here is a screenshot of when I tried to scan my home router: ConclusionAwesome! It finished scanning 5000 ports in less than 2 seconds! You can use the default range (1 to 65535), which will take a few seconds to finish. If you see your scanner is freezing on a single port, that's a sign you need to decrease your number of threads. If the server you're probing has a high ping, you should reduce Port scanning proves to be useful in many cases. An authorized penetration tester can use this tool to see which ports are open and reveal the presence of potential security devices such as firewalls, as well as test the network security and the strength of a device. It is also a popular reconnaissance tool for hackers that are seeking weak points to gain access to the target machine. Most penetration testers often use Nmap to scan ports, as it does not just provide port scanning, but shows services and operating systems that are running, and much more advanced techniques. In our Ethical Hacking with Python EBook, we've built a port scanner with Nmap along with other 35 hacking tools & scripts. Make sure to check it out here if you're interested! You can check the full version of both scripts here. Disclaimer: Note that this script is intended for individuals to test on their devices and learn Python. I will take no responsibility if it is misused. Learn Also: How to Brute Force FTP Servers in Python. Happy Scanning ♥ Loved the article? You'll love our Code Converter even more! It's your secret weapon for effortless coding. Give it a whirl!
Sharing is caring!
Read alsoHow to Make a Network Scanner using Scapy in PythonBuilding a simple network scanner using ARP requests and monitor the network using Scapy library in Python.
How to Make a Subdomain Scanner in PythonLearning how to build a Python script to scan for subdomains of a given domain using requests library.
How to Create a Reverse Shell in PythonBuilding a reverse shell in Python using sockets that can execute remote shell commands and send the results back to the server. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |