From 03c04d599eacbc912e5b2d57e9cb18ba6868b779 Mon Sep 17 00:00:00 2001 From: 4shen0ne <4shen.01@gmail.com> Date: Fri, 1 Nov 2024 08:51:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20improve=20to=5Fcsv=5Ffile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nmass/model/elements.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nmass/model/elements.py b/nmass/model/elements.py index 03478ca..bb52a88 100644 --- a/nmass/model/elements.py +++ b/nmass/model/elements.py @@ -1,6 +1,6 @@ import csv from io import BufferedWriter -from typing import Literal, Optional +from typing import Literal, Optional, Union from urllib.request import urlopen import lxml.etree as ET @@ -212,8 +212,13 @@ def to_html(self, xslt_path: str = "https://nmap.org/svn/docs/nmap.xsl", pretty_ newdom = transform(self.to_xml_tree()) # type: ignore return ET.tostring(newdom, pretty_print=pretty_print).decode() - def to_csv_file(self, file: BufferedWriter) -> None: - writer = csv.writer(file) # type: ignore + def to_csv_file(self, file: BufferedWriter, dialect: Union[str, csv.Dialect] = "excel") -> None: + """Write information to a CSV file. + + :param file: A file-like object where the CSV data will be written + :param dialect: The CSV dialect used for formatting the file, defaults to "excel" + """ + writer = csv.writer(file, dialect=dialect) # type: ignore writer.writerow(["IP", "Port", "Protocol", "State", "Service", "Reason", "Product", "Version", "CPE"]) for host in self.hosts: host_info = host.address[0].addr