【发布时间】:2021-05-26 20:39:11
【问题描述】:
我有一个 CSV 文件,列中包含主机地址,行中包含它们的端口,我想遍历列,然后扫描行中的相应端口。
我想出了这段代码,如果我手动使用带有主机 IP 和端口的单元,这将有效。
import socket
import csv
lst = [1,2,3,4,5,6,7,8,9]
line_number = 0
while line_number < len(lst):
line_number = int(line_number +1)
with open('temp.csv', 'rt') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
h = mycsv[line_number][0]
line_number = int(line_number +1)
while line_number < len(lst):
line_number = int(line_number +1)
with open('temp.csv', 'rt') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
p = mycsv[line_number][2]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = h
port = int(p)
def portScanner(port):
if s.connect_ex((host, port)):
print("Closed")
else:
print("Open")
portScanner(port)
CSV 示例
ip,port
1.1.1.1,80,443,22
2.2.2.2,80,21,22
3.3.3.3,111,22,21
.
.
.
.
谢谢!
【问题讨论】:
标签: python python-3.x list csv for-loop