我一直在关注使用python进行端口扫描的一些教程(这使用线程),无论我做什么,都说所有端口都关闭了。我知道这不可能是因为端口80(网络)对这个网站是开放的,我发现一个在线工具说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()
答案 0 :(得分:2)
你应该抓住异常并阅读它:
except Exception as e:
with print_lock:
print("port",port,"is closed due to " + str(e))
有了它,你可以找到错误。您的错误是"服务器'未定义"。
并且connect
方法接受元组,所以你应该这样做
con = s.connect((target, port))
它有效!