[英]How to read column and rows with Python and iterate through the entries?
我有一個 CSV 文件,列中有主機地址,行中有它們的端口,我想通過列 go 然后掃描行中的相應端口。
我想出了這段代碼,如果我手動使用帶有主機 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
.
.
.
.
謝謝!
首先使用next()
跳過你 header 。 然后,您可以通過首先獲取ip
地址然后使用 Python 的*
運算符將所有其他條目作為ports
讀取來讀取每一行。 例如:
例如:
import csv
import socket
def portScanner(ip, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if s.connect_ex((ip, port)):
print(f" Port {port}: Closed")
else:
print(f" Port {port}: Open")
with open('temp.csv') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
for ip, *ports in csv_input:
print(f"IP: {ip}")
for port in ports:
portScanner(ip, int(port))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.