gpt4 book ai didi

python - 端口扫描器需要元组而不是 str

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:16 24 4
gpt4 key购买 nike

好的,基本上我决定尝试构建一个基本的端口扫描器。这是我的代码:

(上面还有更多,但觉得贴在这里太多了)

##Print port menu
print "-"*60
print "Specify ports"
print "-"*60
print """
1)Use default list
2)Specify your own port list\n"""
print"-"*60
menu2=raw_input("Please choose an option\n")
##Define default port list
default_list="21, 22,23, 24, 25, 80, 110, 135, 139, 443, 445, 553, 3306, 3389, 8080"

##Set port list to default if option "1" is chosen
if menu2 == "1":
port_list='default_list'

##Request user port list if option "2" is chosen
if menu2 == "2":
port_list=raw_input("Please enter the ports you would like scanned.\neg. 22, 23\n")

print "Ok, here we go"

for i in str(port_list):
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

results = connection.connect_ex(ip + i)

这对我来说看起来不错,但是当我运行它时我收到一个错误“AF_INET 地址必须是元组,而不是 str”我不确定哪一部分是问题所在?是我的默认端口字符串引发错误吗?还是我的 ip 地址格式有问题??我尝试了一些我的设备 IP 地址,但总是得到同样的错误。

最佳答案

来自documentation :

A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.

因此,假设您的 ip 变量是像“100.50.200.5”这样的 IP 地址,而 i 是一个带有端口号的整数,您的连接代码应该如下所示

result = connection.connect_ex((ip, i))

但是,看起来您的循环在 port_list 变量(一个字符串)上进行交互。要使其正常工作,您需要将此字符串(例如“22, 23”)转换为整数列表 ([22, 23])。

例如,您可以这样重写代码:

##Print port menu
print "-"*60
print "Specify ports"
print "-"*60
print """
1)Use default list
2)Specify your own port list\n"""
print"-"*60
menu2=raw_input("Please choose an option\n")
##Define default port list
default_list="21, 22,23, 24, 25, 80, 110, 135, 139, 443, 445, 553, 3306, 3389, 8080"

##Set port list to default if option "1" is chosen
if menu2 == "1":
port_list = default_list

##Request user port list if option "2" is chosen
if menu2 == "2":
port_list = raw_input("Please enter the ports you would like scanned.\neg. 22, 23\n")

print "Ok, here we go"

port_list = [int(port.strip()) for port in port_list.split(',')]

for port in port_list:
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
results = connection.connect_ex((ip, port))

关于python - 端口扫描器需要元组而不是 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com