Python Forum

Full Version: 'NoneType' object has no attribute 'encode'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hii,
When I run this code in terminal I get an error as 'NoneType' object has no attribute 'encode'


# -*- coding: utf-8 -*-
# Copyright (C) 2019 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from base64 import b64decode
from pathlib import Path


def check_args(args):
    len_args = len(args.script) - 1
    if len_args < 1:
        message = """
        This script requests the given report and saves it as a pdf file locally.
        It needs one parameters after the script name.

        1. <report_id>     -- ID of the report
        
        Optional a file name to save the pdf in.

        Example:
            $ gvm-script --gmp-username name --gmp-password pass \
ssh --hostname <gsm> scripts/pdf-report.gmp.py <report_id> <pdf_file>
        """
        print(message)
        quit()


def main(gmp, args):
    # check if report id and PDF filename are provided to the script
    # argv[0] contains the script name
    check_args(args)

    report_id = args.argv[1]
    if len(args.argv) == 3:
        pdf_filename = args.argv[2]
    else:
        pdf_filename = args.argv[1] + ".pdf"

    pdf_report_format_id = "c402cc3e-b531-11e1-9163-406186ea4fc5"

    response = gmp.get_report(
        report_id=report_id, report_format_id=pdf_report_format_id
    )

    report_element = response.find("report")
    # get the full content of the report element
    content = report_element.find("report_format").tail

    # convert content to 8-bit ASCII bytes
    binary_base64_encoded_pdf = content.encode('ascii')

    # decode base64
    binary_pdf = b64decode(binary_base64_encoded_pdf)

    # write to file and support ~ in filename path
    pdf_path = Path(pdf_filename).expanduser()

    pdf_path.write_bytes(binary_pdf)

    print('Done. PDF created: ' + str(pdf_path))


if __name__ == '__gmp__':
    main(gmp, args)
The command I run in terminal is as follows:
gvm-script --gmp-username admin --gmp-password admin socket --DESKTOP-9E60Q4 pdf-report.gmp.py 571b5828-12fe-4b78-9c55-2ff0095ed385


The error I received is as follows

Error:
'NoneType' object has no attribute 'encode'
Someone please help me solving this error, it is needed for my project.
(Nov-05-2020, 06:05 AM)bhagyashree Wrote: [ -> ]Hii,
When I run this code in terminal I get an error as 'NoneType' object has no attribute 'encode'


# -*- coding: utf-8 -*-
# Copyright (C) 2019 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from base64 import b64decode
from pathlib import Path


def check_args(args):
    len_args = len(args.script) - 1
    if len_args < 1:
        message = """
        This script requests the given report and saves it as a pdf file locally.
        It needs one parameters after the script name.

        1. <report_id>     -- ID of the report
        
        Optional a file name to save the pdf in.

        Example:
            $ gvm-script --gmp-username name --gmp-password pass \
ssh --hostname <gsm> scripts/pdf-report.gmp.py <report_id> <pdf_file>
        """
        print(message)
        quit()


def main(gmp, args):
    # check if report id and PDF filename are provided to the script
    # argv[0] contains the script name
    check_args(args)

    report_id = args.argv[1]
    if len(args.argv) == 3:
        pdf_filename = args.argv[2]
    else:
        pdf_filename = args.argv[1] + ".pdf"

    pdf_report_format_id = "c402cc3e-b531-11e1-9163-406186ea4fc5"

    response = gmp.get_report(
        report_id=report_id, report_format_id=pdf_report_format_id
    )

    report_element = response.find("report")
    # get the full content of the report element
    content = report_element.find("report_format").tail

    # convert content to 8-bit ASCII bytes
    binary_base64_encoded_pdf = content.encode('ascii')

    # decode base64
    binary_pdf = b64decode(binary_base64_encoded_pdf)

    # write to file and support ~ in filename path
    pdf_path = Path(pdf_filename).expanduser()

    pdf_path.write_bytes(binary_pdf)

    print('Done. PDF created: ' + str(pdf_path))


if __name__ == '__gmp__':
    main(gmp, args)
The command I run in terminal is as follows:
gvm-script --gmp-username admin --gmp-password admin socket --DESKTOP-9E60Q4 pdf-report.gmp.py 571b5828-12fe-4b78-9c55-2ff0095ed385


The error I received is as follows

Error:
'NoneType' object has no attribute 'encode'
Someone please help me solving this error, it is needed for my project.
The screenshot of output which I received is as follows

https://postimg.cc/zyG9LFj2
Someone please help I have also added image of terminal output error
As requested by buran in first post, Please provide the
original unmodified and complete error traceback, everything, and enclose in error tags (not quotes).
Please read: bbcode
Not my cup of tea but I observe that function check_args should return None as it does not have return or yield. This function is called in first line in body of function main().
An error message without context is not very useful. There is only one place "encode" is used in this code.
    report_element = response.find("report")
    # get the full content of the report element
    content = report_element.find("report_format").tail
 
    # convert content to 8-bit ASCII bytes
    binary_base64_encoded_pdf = content.encode('ascii')
The message you are receiving would indicate that "content" is None, which would indicate that this report_element.find("report_format").tail returns None.

I do not know why that would return None. Is the problem that "report_element" could not find "report_format" and thus the "tail" is not defined? Why would that be? Is this what happens when the requested report could not be found? We have no way to know since we don't know anything at all about pdf-report.gmp.py.

This code looks like it is part of a package, a commercial software package. I assume the code works, but you have not provided that information. If the code works then the problem is either that it cannot find the report, or that the report is not in a format that could be processed. In either case I would expect better reporting, but the script just assumes everything is fine and doesn't appear to do any error reporting at all.

Sorry that I can't help you any more.