Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnicodeDecodeError . . .
#1
Question 
When making a request for a file in .png format through the browser url I receive the following message ( UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte ), below follows the complete output of the error:

Running Kosmos Application
Browser Access - http://127.0.0.1:8000
Crl+c for flow command or Crl+z for stop
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET / HTTP/1.1" 200 11522
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/css/materialize.ext.css HTTP/1.1" 200 9116
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/js/jquery.ext.js HTTP/1.1" 200 37
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/fgs/wsi.svg HTTP/1.1" 200 286214
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/fgs/cc-webstrucs.svg HTTP/1.1" 200 11882
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/fgs/w3c-consortium.svg HTTP/1.1" 200 3587
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/fgs/w3c-semantic.svg HTTP/1.1" 200 14439
127.0.0.1 - - [27/Feb/2021 10:58:19] "GET /website/assets/fgs/html5-semantic.svg HTTP/1.1" 200 2289
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/spark/add HTTP/1.1" 200 12442
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/css/materialize.ext.css HTTP/1.1" 200 13389
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/fgs/wsi.svg HTTP/1.1" 200 286214
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /website/assets/js/jquery.ext.js HTTP/1.1" 200 37
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/fgs/cc-webstrucs.svg HTTP/1.1" 200 11882
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/fgs/w3c-consortium.svg HTTP/1.1" 200 3587
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/fgs/w3c-semantic.svg HTTP/1.1" 200 14439
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/fgs/html5-semantic.svg HTTP/1.1" 200 2289
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "runwsgi.py", line 43, in application
    return [str(o_get.spark(start_response, v_uri)).encode("utf-8")]
  File "/home/joao/PycharmProjects/kosmos/cern/gear/requestget.py", line 61, in spark
    return o_get.m_get(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/safe/detectthreatstart.py", line 38, in m_get
    return o_get.m_get(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/safe/detectxssthreat.py", line 59, in m_get
    return o_get.m_get(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/safe/detectsqlithreat.py", line 49, in m_get
    return o_get.spark(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/flow/get/requesttypesget.py", line 61, in spark
    return o_get.spark(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/flow/get/requestclientfiles.py", line 702, in spark
    s_file = o_file.read()
  File "/usr/local/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
127.0.0.1 - - [27/Feb/2021 10:58:22] "GET /account/assets/fgs/cloud.png HTTP/1.1" 500 59
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "runwsgi.py", line 43, in application
    return [str(o_get.spark(start_response, v_uri)).encode("utf-8")]
  File "/home/joao/PycharmProjects/kosmos/cern/gear/requestget.py", line 61, in spark
    return o_get.m_get(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/safe/detectthreatstart.py", line 38, in m_get
    return o_get.m_get(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/safe/detectxssthreat.py", line 59, in m_get
    return o_get.m_get(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/safe/detectsqlithreat.py", line 49, in m_get
    return o_get.spark(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/flow/get/requesttypesget.py", line 61, in spark
    return o_get.spark(start_response, v_uri)
  File "/home/joao/PycharmProjects/kosmos/cern/flow/get/requestclientfiles.py", line 702, in spark
    s_file = o_file.read()
  File "/usr/local/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
127.0.0.1 - - [27/Feb/2021 10:58:29] "GET /account/assets/fgs/cloud.png HTTP/1.1" 500 59
What is this error about and how can I resolve it ?
Reply
#2
Python tries to decode the content of the file as utf-8. You are presumably opening it with "open(path-to-file)" or "open(path-to-file, 'r')" but ought to open it with "open(path-to-file, 'rb')", 'b' for binary.
Reply
#3
(Feb-27-2021, 04:41 PM)Serafim Wrote: Python tries to decode the content of the file as utf-8. You are presumably opening it with "open(path-to-file)" or "open(path-to-file, 'r')" but ought to open it with "open(path-to-file, 'rb')", 'b' for binary.

The error message stopped being displayed on the terminal, but the problem is that the figure does not render (black background appears with a white square in the middle) follows the screen print:

[Image: view?usp=sharing]

Here is the changed code:

# faz o loop no array comparando as extensões :
        while v_ext in a_ext:
            # Atribui path ao diretorio de arquivos estaticos, memoriza path na variable :
            v_filedir = "web/" + v_app + "/" + v_ass + "/"

# Compara se extensões são as mesmas :
            elif v_ext == ".png":
                # Acessa o arquivo static requisitado :
                o_file = open(v_filedir + "fgs/" + o_fame, "rb")
                # Faz a leitura do arquivo :
                s_file = o_file.read()
                # Variable : obtem status de resposta para client browser :
                status = "200 OK"
                # Variable : obtem cabeçalho http para o browser do client
                headers = [("Content-type", "image/png; charset=utf-8")]
                # Function : Envio de variable para o client browser :
                start_response(status, headers)
                # Output a requisição do client browser:
                return s_file
Reply
#4
Can't see the image but the point is that more of the code used to render the image is needed to understand the rendering problem.
Reply
#5
(Feb-27-2021, 07:31 PM)Serafim Wrote: Can't see the image but the point is that more of the code used to render the image is needed to understand the rendering problem.

Link figure original : https://drive.google.com/file/d/1B0LEggm...sp=sharing

Link figuro error : https://drive.google.com/file/d/1zeh8jeH...sp=sharing

Being in doubt is problems reading the file (.png) because I can render using another http request.
Reply
#6
(Feb-27-2021, 08:47 PM)JohnnyCoffee Wrote:
(Feb-27-2021, 07:31 PM)Serafim Wrote: Can't see the image but the point is that more of the code used to render the image is needed to understand the rendering problem.

Link figure original : https://drive.google.com/file/d/1B0LEggm...sp=sharing

Link figuro error : https://drive.google.com/file/d/1zeh8jeH...sp=sharing

Being in doubt is problems reading the file (.png) because I can render using another http request.

I am aware that there are some third party modules for reading with the extension (.png), is there any functional way that I can open and view the .png file natively from python without having problems?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 16: invalid cont Melcu54 3 4,980 Mar-26-2023, 12:12 PM
Last Post: Gribouillis
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,948 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,450 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  open(file, 'rb') raises UnicodeDecodeError binnybit 1 2,474 Sep-28-2020, 06:55 AM
Last Post: Gribouillis
  UnicodeDecodeError: pratheep 0 1,844 May-06-2020, 05:46 AM
Last Post: pratheep
  unicodedecodeerror:utf codec can't decode byte 0xe3 in position 1 mariolopes 3 2,822 Oct-14-2019, 10:17 PM
Last Post: mariolopes

Forum Jump:

User Panel Messages

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