我正在制作一个端口扫描器通过此消息这里是我的代码,但它检查一个端口21我已粘贴输出
import socket
import os
host = input("Enter the host name or ip : ")
s = socket.socket()
s.settimeout(5)
p = 0;
s.close
port = [21,22,23,25,53,80,110,115,135,139,143,194,443,445,1433,3306,3389,5632,5900,6112]
while(p<=19):
try:
s.connect(('host', port[p]))
except ConnectionRefusedError:
print("Port %d is close" %(port[p]))
except socket.timeout:
print("Port %d is close" %(port[p]))
else:
print("Port %d is open" %(port[p]))
p=p+1;
s.close
在命令行上:
PS E:\Codes by me\Selenium py> python .\practice.py
Enter the host name or ip : 89.86.98.76
Port 21 is close # it checks one port
Traceback (most recent call last):
File ".\practice.py", line 11, in <module>
s.connect((host, port[p]))
OSError: [WinError 10022] An invalid argument was supplied
答案 0 :(得分:1)
您正在传递文字字符串'host'
作为主机。您应该传递变量host
:
s.connect((host, port[p]))
您每次都没有关闭套接字,因为您在s.close()
中省略了括号。但是,如果每次都关闭套接字,则必须每次都创建一个新套接字,而不是尝试重用相同的套接字。您无法重复使用已关闭的套接字。