我怎样才能让我的端口扫描器工作

How can I get my port scanner to work

我正在尝试制作一个端口扫描器,该扫描器会根据 10-255 范围内的所有奇数 IP 地址搜索输入的端口。

我当前的代码不工作,我收到了这个错误;

error  str, bytes or bytearray expected, not int

我以为 s.connect((int(ipaddress.ip_address(my_net[i])), port)) 会解决这个问题,但没有。

我错过了什么吗?

我当前的代码如下:

import socket
import ipaddress
import subprocess
import sys
from datetime import datetime
#define the subnet to scan
subnet=input("which subnet are you scanning, please enter in x.x.x ")

my_net =[]
count =0
for i in range(11,255):  
    if i%2!=0:
        my_net.insert(count,(subnet+"." +str(i)))

print("Your selected network is " , subnet , "below are the usable Ip addresses")
#the user is to select the port that will be scanned as a part of the test
port = input("Enter the number of the port you would like to scan ")
# Print a banner with information on which host we are about to scan
print ("-" * 60)
print ("Please wait, scanning network" , subnet ,".0/24")
print ("-" * 60)
#check time now#
t1 = datetime.now()
#output. Confirm if the port is open or closed

for i in range(len(my_net)):
        try:
            socket.setdefaulttimeout (2)
            s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((int(ipaddress.ip_address(my_net[i])), port))
            banner=s.recv(1024)
            print(banner)
        except Exception as e:
            print("error " , e)

# Checking the time again
t2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total =  t2 - t1
print ('Scanning Completed in: ', total)

我想你会发现 ip 地址必须是一个字符串。例如 '127.0.0.1' 不是 int,但端口是 int。