'Python: Port Scanner -- No port is shown

import socket
from IPy import IP

def scan(target, port_num):
    converted_ip = check_ip(target)
    print(f"\n [-_0 Scanning Target]: {target}")
    for port in range(1, port_num):
        scan_port(converted_ip, port)

def check_ip(ip):
    try:
        IP(ip)
        return (ip)
    except ValueError:
        return socket.gethostbyname(ip)

def get_banner(s):
    return s.recv(1024)

def scan_port(ipaddress, port):
    try:
        sock = socket.socket()
        sock.settimeout(0.5)
        sock.connect((ipaddress, port))
        try:
            banner = get_banner(sock)
            print(f"[+] Open Port {port}: {banner.decode().strip()}")
        except:
            print(f"[+] open Port {port}")
    except:
        pass

if __name__ == "__main__":
    targets = input("[+] Enter Targets to Scan (split multiple targets with ,): ")
    port_num = input("Enter Numbers of Ports you want to Scan: ")
    if ',' in targets:
        for ip_add in targets.split(","):
            scan(ip_add.strip(" "), port_num)
    else:
        scan(targets, port_num)

guys, I wrote a simple port scanner as above, but whenever I tried to scan the ip address(IPv$) of my computer, or linux system in my vitual machine, no open port is shown.

Q1. Can someone tell me how to fix this? Thank you for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source