Last modified: Apr 27, 2025 By Alexander Williams
Scan Open Ports on IP Address Using Python
Port scanning helps identify open ports on a network device. It is useful for security checks and troubleshooting. Python makes it easy to create a port scanner.
What Is Port Scanning?
Port scanning checks which ports are open on a target IP. Open ports indicate running services. This helps in network analysis and security assessments.
Common ports include 80 (HTTP), 443 (HTTPS), and 22 (SSH). Scanning them reveals potential vulnerabilities.
Python Socket Programming Basics
Python's socket
module allows network communication. It helps create connections to IP addresses and ports. This is key for building a port scanner.
Before scanning, ensure you have Python installed. Check out how to install IPython for a better coding environment.
Simple Port Scanner in Python
Here's a basic script to scan ports on an IP address. It uses socket
to check connection status.
import socket
def scan_port(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((ip, port))
sock.close()
return result == 0
except:
return False
target_ip = "127.0.0.1" # Replace with target IP
ports_to_scan = [80, 443, 22, 21] # Common ports
for port in ports_to_scan:
if scan_port(target_ip, port):
print(f"Port {port} is open")
else:
print(f"Port {port} is closed")
Port 80 is open
Port 443 is open
Port 22 is closed
Port 21 is closed
How the Code Works
The scan_port
function checks if a port is open. It creates a socket and tries to connect. A timeout of 1 second prevents long waits.
Important: Always respect network policies. Unauthorized scanning may be illegal.
Enhancing the Port Scanner
You can improve the scanner by adding more features. For example, scan a range of ports or use threading for speed.
Learn about getting all IP addresses on a local network to expand your scanner's capabilities.
import socket
from concurrent.futures import ThreadPoolExecutor
def scan_port(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((ip, port))
sock.close()
return port if result == 0 else None
except:
return None
def scan_ports(ip, start_port, end_port):
open_ports = []
with ThreadPoolExecutor(max_workers=100) as executor:
futures = [executor.submit(scan_port, ip, port) for port in range(start_port, end_port + 1)]
for future in futures:
result = future.result()
if result:
open_ports.append(result)
return open_ports
target_ip = "127.0.0.1"
open_ports = scan_ports(target_ip, 1, 1024)
print(f"Open ports: {open_ports}")
Security Considerations
Port scanning can be seen as a hostile act. Always get permission before scanning networks. Use this knowledge ethically.
For secure coding, validate IP addresses first. Read our guide on validating IPv4 and IPv6 addresses.
Conclusion
Python makes port scanning simple with the socket
module. This guide showed basic and advanced scanning techniques. Always use these tools responsibly.
Remember to handle errors properly. For common Python errors, check our guides on TypeError solutions.