Lazo Lab Sign up

Python Projects & Skills / Security tools in Python

Lesson 27 of 28

Port checker for your own machine

The socket module can check whether a port on your own computer is open. Only scan machines you own or have permission to test.

Key points

  • socket.connect_ex returns 0 if open
  • Set a short timeout
  • Only test your own devices
import socket
s = socket.socket()
s.settimeout(0.5)
print("open" if s.connect_ex(("127.0.0.1", 8000)) == 0 else "closed")
Watch a video on thisOpens YouTube search results for “Port checker for your own machine” in a new tab

Quiz · +10 XP

Whose machines may you scan for open ports?

Log in to save progress and earn XP.