
import socket

# تابع بررسی پورت
def check_port(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(1)  # تنظیم زمان انتظار برای اتصال (1 ثانیه)
    result = s.connect_ex((host, port))
    s.close()
    return result == 0

# بررسی پورت‌ها در یک حلقه
def check_open_ports(host, start_port, end_port):
    open_ports = []
    for port in range(start_port, end_port + 1):
        if check_port(host, port):
            open_ports.append(port)
    return open_ports

# تنظیمات
host = "127.0.0.1"  # آدرس میزبان (در اینجا 127.0.0.1 برای localhost)
start_port = 1       # شروع از پورت 1
end_port = 1024      # پایان در پورت 1024

# بررسی پورت‌های باز
open_ports = check_open_ports(host, start_port, end_port)

# نمایش پورت‌های باز
if open_ports:
    print(f"پورت‌های باز روی {host}: {open_ports}")
else:
    print(f"هیچ پورت باز روی {host} پیدا نشد")
