#!/usr/bin/python3
import socket
ip = input('Enter the IP address: ')
port = input('Enter the Port: ')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if s.connect_ex((ip,port)):
print ('Port', port, 'is closed')
else:
print ('Port', port, 'is open')
正如您所看到的,它是一个非常简单的端口扫描程序,我试图运行(目前正在学习一些渗透测试)。
这是我得到的错误:
Traceback (most recent call last):
File "./python.py", line 9, in <module>
if s.connect_ex((ip,port)):
TypeError: an integer is required (got type str)
我假设我需要将第9行设置为整数 - 我该怎么做?
提前干杯。
答案 0 :(得分:1)
此处:port = input('Enter the Port: ')
您读取了str
的用户输入,并将其作为端口号传递(应该是您可能已经猜到int
)。你的回溯告诉你完全一样的事情。您需要通过调用int(port)
答案 1 :(得分:0)
port = int(input('Enter the Port: '))
答案 2 :(得分:0)
input()
返回一个字符串,您需要将其转换为整数。将字符串转换为有效整数可能是一项相当复杂的任务。对于第一次改变方法
port = input('Enter the Port: ')
到
port = input('Enter the Port: ')
try:
port = int(port)
except ValueError as err:
print('Invalid input. Please try again.')