HackPath Academy

Programming & Scripting for Pentesters

Python for Pentesters (+ Build a Port Scanner)

The offensive Python toolkit — sockets, requests, and your first tool.

Intermediate · 18 min

Open the interactive lesson →

Python is the lingua franca of offensive security. Its sockets, requests library, and huge ecosystem let you build almost any tool quickly. Let's build a real (if simple) TCP port scanner.

import socket
from concurrent.futures import ThreadPoolExecutor

def scan(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(0.5)
    if s.connect_ex((host, port)) == 0:
        print(f'[+] {port} open')
    s.close()

host = '127.0.0.1'
with ThreadPoolExecutor(max_workers=200) as pool:
    for port in range(1, 1025):
        pool.submit(scan, host, port)

Libraries you'll reach for constantly

Take the quiz →