Pr Mod
Pr Mod

Reputation: 261

Port scan fails when firewall is enabled

I have windows client system environment , when i do port scan with below command i do not get response if firewall is turned on.

Q1. i would like to know if any method i can get response whenever if firewall turned on or not? Q2. Any port is always open in a windows system so that i can get response.

Note: Disable Firewall option is not suitable for me as when ever i do change computer domain settings firewall turned on by default for domain Networks in firewall

enter image description here

import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ClientIP = '10.xxx.xxx.xxx'
def portscanner(port):
    try:
        s.connect((ClientIP,port))
        return True
    except:
        return False
for x in range(8000,8003):
    if portscanner(x):
        print("Port is open",x)
    else:
        print("port is closed",x)

Upvotes: 0

Views: 283

Answers (1)

Barmar
Barmar

Reputation: 780889

When the port is blocked by a firewall, you can't detect the failure until a timeout. You can set a short timeout.

s.settimeout(1);

This will only wait 1 second before giving up.

Upvotes: 1

Related Questions