-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportScanner.py
More file actions
30 lines (23 loc) · 805 Bytes
/
Copy pathportScanner.py
File metadata and controls
30 lines (23 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import socket
def scan_ports(target):
print(f"Scanning target: {target}")
print("-" * 50)
try:
for port in range(1, 1025):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(0.2)
result = s.connect_ex((target, port))
if result == 0:
print(f"Port {port} is open")
s.close()
except KeyboardInterrupt:
print("\nExiting program.")
except socket.gaierror:
print("\nHostname could not be resolved.")
except socket.error:
print("\nServer not responding.")
print("-" * 50)
print("Scanning finished.")
if __name__ == "__main__":
target_host = input("Enter the IP address or hostname to scan: ")
scan_ports(target_host)