Problemas de codificación encontrados por flask-mail

Primero encontré el siguiente error:

[2023-07-17 00:00:31,202] ERROR in app: Exception on /test [GET]
Traceback (most recent call last):
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\app.py", line 46, in send_mail
    mail.send(msg)
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
                ^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 163, in configure_host
    host.starttls()
  File "D:\software\Python311\Lib\smtplib.py", line 769, in starttls
    self.ehlo_or_helo_if_needed()
  File "D:\software\Python311\Lib\smtplib.py", line 611, in ehlo_or_helo_if_needed
    if not (200 <= self.ehlo()[0] <= 299):
                   ^^^^^^^^^^^
  File "D:\software\Python311\Lib\smtplib.py", line 451, in ehlo
    self.putcmd(self.ehlo_msg, name or self.local_hostname)
  File "D:\software\Python311\Lib\smtplib.py", line 378, in putcmd
    self.send(f'{s}{CRLF}')
  File "D:\software\Python311\Lib\smtplib.py", line 357, in send
    s = s.encode(self.command_encoding)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-11: ordinal not in range(128)

Cuando encuentre este error, modifique inmediatamente command_encoding en la pila de errores anterior, de la siguiente manera:

    def send(self, s):
        """Send `s' to the server."""
        if self.debuglevel > 0:
            self._print_debug('send:', repr(s))
        if self.sock:
            if isinstance(s, str):
                # send is used by the 'data' command, where command_encoding
                # should not be used, but 'data' needs to convert the string to
                # binary itself anyway, so that's not a problem.
                s = s.encode(self.command_encoding)
            sys.audit("smtplib.send", self, s)

Como se muestra arriba, modifique self.command_encoding en el archivo smtplib.py y cámbielo directamente a 'utf8'. Después de la modificación, reinicie el proyecto y se informará el siguiente error:

[2023-07-17 00:02:35,060] ERROR in app: Exception on /test [GET]
Traceback (most recent call last):
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask\app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\app.py", line 46, in send_mail
    mail.send(msg)
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 492, in send
    message.send(connection)
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 427, in send
    connection.send(self)
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 190, in send
    message.as_bytes() if PY3 else message.as_string(),
    ^^^^^^^^^^^^^^^^^^
  File "D:\Desktop\GitHub\personal-information-platform\venv\Lib\site-packages\flask_mail.py", line 385, in as_bytes
    return self._message().as_bytes()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\software\Python311\Lib\email\message.py", line 208, in as_bytes
    g.flatten(self, unixfrom=unixfrom)
  File "D:\software\Python311\Lib\email\generator.py", line 116, in flatten
    self._write(msg)
  File "D:\software\Python311\Lib\email\generator.py", line 199, in _write
    self._write_headers(msg)
  File "D:\software\Python311\Lib\email\generator.py", line 422, in _write_headers
    self._fp.write(self.policy.fold_binary(h, v))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\software\Python311\Lib\email\policy.py", line 202, in fold_binary
    return folded.encode(charset, 'surrogateescape')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode characters in position 56-58: ordinal not in range(128)

Luego vaya al archivo Policy.py y modifique la siguiente información.

        folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
        charset = 'utf8' if self.utf8 else 'ascii'
        return folded.encode(charset, 'surrogateescape')

En el archivo Policy.py con el problema anterior, modifique la parte del conjunto de caracteres como se indica arriba y elimine charset = 'utf8' if self.utf8 else 'ascii'la parte, y luego estará bien. Es realmente molesto que no pueda encontrar la respuesta que quiero en varios Baidu. Grábalo.

Supongo que te gusta

Origin blog.csdn.net/weixin_44948269/article/details/131757172
Recomendado
Clasificación