Skip to content

Instantly share code, notes, and snippets.

@andostronaut
Created August 14, 2024 08:12
Show Gist options
  • Save andostronaut/bb79bc8c7dca99232d5f294e0d6f2cfd to your computer and use it in GitHub Desktop.
Save andostronaut/bb79bc8c7dca99232d5f294e0d6f2cfd to your computer and use it in GitHub Desktop.
Port scanning in Python
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