GREM
GREM

Reputation: 13

Python port scanning all closed

I have been following some tutorials on port scanning with python (this uses threading) and no matter what i do it says that all ports are closed. And I know this cant be because port 80 (web) is open for this site and an online tool i found says that 22 and 80 are open. What should I do?

import socket
import threading
from queue import Queue
import time

print_lock = threading.Lock()

target = 'www.pythonprogramming.net'

def portscan(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        con = s.connect(server, port)
        with print_lock:
            print("port",port,"is open")
        con.close()
    except:
        pass
        with print_lock:
            print("port",port,"is closed")

def threader():
    while True:
        worker = q.get()
        portscan(worker)
        q.task_done()

q = Queue()

for  i in range(30):
    t = threading.Thread(target = threader)
    t.deamon = True
    t.start()

for worker in range(1,101):
    q.put(worker)

q.join()

Upvotes: 0

Views: 165

Answers (1)

user996142
user996142

Reputation: 2883

You should catch exception and read it:

except Exception as e:
    with print_lock:
        print("port",port,"is closed due to " + str(e))

With it, you can find the error. Your error is "server' is not defined".

And connect method accepts tuple, so you should do

con = s.connect((target, port))

And it works!

Upvotes: 2

Related Questions