Python端口扫描全部关闭

Python port scanning all closed

我一直在关注一些关于使用 python 进行端口扫描的教程(这使用线程),但无论我做什么,它都说所有端口都已关闭。我知道这不可能是因为端口 80 (web) 对这个网站是开放的,而且我发现的一个在线工具说 22 和 80 是开放的。我该怎么办?

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()

你应该捕获异常并阅读它:

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

有了它,你就可以找到错误了。你的错误是 "server' is not defined".

connect方法接受元组,所以你应该

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

而且有效!