Created
August 14, 2024 08:12
-
-
Save andostronaut/bb79bc8c7dca99232d5f294e0d6f2cfd to your computer and use it in GitHub Desktop.
Port scanning in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
def port_scanner(host, ports): | |
hostname = socket.gethostbyname(host) | |
print(f'Scanning {host}: {hostname}') | |
for port in ports: | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
socket.setdefaulttimeout(1) | |
result = sock.connect_ex((hostname, port)) | |
if result == 0: | |
print(f'Port {port} is open') | |
else: | |
print(f'Port {port} is closed') | |
sock.close() | |
host = '127.0.0.1' | |
ports = [22, 80. 443, 3000] | |
port_scanner(host, ports) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment