我正在尝试制作一个端口扫描器,用户可以在其中键入一系列端口以在主机上进行扫描,我将输入从 str 转换为 int 以获取范围,但它仍然说它是一个 str。这是我的代码:
os.system('cls')
host = raw_input('Enter hostname or IP address: ')
target = socket.gethostbyname(host)
# converts hostname to IP address
portRange1 = raw_input("Please enter the first number (x) in your range (x, y): ")
portRange2 = raw_input("Please enter the second number (y) in your range (" + portRange1 + ", y): ")
# asks user for range of ports to scan
portRange1 = int(portRange1)
portRange2 = int(portRange2)
# converts variables from str to int
os.system('cls')
# clears console screen
print 'Starting scan on host ' + target
for port in range(portRange1 + ", " + portRange2):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((target, port))
if result == 0:
print "Port {}: Open".format(port)
sock.close()
choice()
# scans for ports 0-1025 on host
choice()
我的错误是:
File "swisshack_W2.py", line 61, in portScanner
for port in range(portRange1, ", ", portRange2):
TypeError: range() integer end argument expected, got str.