Python Forum
'NoneType' object has no attribute 'encode'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NoneType' object has no attribute 'encode'
#1
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.
buran write Nov-05-2020, 06:33 AM:
Please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post
Reply
#2
(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.
Reply
#3
The screenshot of output which I received is as follows

https://postimg.cc/zyG9LFj2
Reply
#4
Someone please help I have also added image of terminal output error
Reply
#5
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
Reply
#6
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().
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 738 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  encode/decode to show correct country letters in a CTk combobox janeik 2 711 Sep-02-2023, 09:46 AM
Last Post: janeik
  TypeError: 'NoneType' object is not callable akbarza 4 986 Aug-24-2023, 05:14 PM
Last Post: snippsat
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,672 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,490 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,868 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,808 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,334 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,593 Feb-17-2023, 06:01 PM
Last Post: Sameer33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020