How to test if a host port is open?

Asked

Viewed 1,638 times

-1

How to find out if a particular port of a network computer is open?

The more efficient the method, the better. I will need to scan an entire subnet several times a day.

  • friend you can use scapy / python-nmap or socket

2 answers

0

The only way to know is to try to open a connection. If the port is not open your application receives an immediate response (error 1225 on Windows and 111 on Linux, corresponding to a TCP RST segment).

0


Using socket:

import socket

def test_port(ip, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    result = sock.connect_ex((ip, port))
    sock.close()

    if result == 0:
        return True
    else:
        return False

Browser other questions tagged

You are not signed in. Login or sign up in order to post.