Recently, an error occurred when deploying an application written with Flask to the official server through Supervisor + uWSGI. The problem was solved by searching for relevant information, so I wanted to share it with you, the following article describes how to solve uWSGI encoding problems. For more information, see. Recently, an error occurred when deploying an application written with Flask to the official server through Supervisor + uWSGI. The problem was solved by searching for relevant information, so I wanted to share it with you, the following article describes how to solve uWSGI encoding problems. For more information, see.
Problems found
I encountered a problem in my recent work. when I deployed the Flask application to the official server through Supervisor + uWSGI, the following error occurs:
Unable to print the message and arguments – possible formatting error.
Or
UnicodeEncodeError: ‘ascii' codec can't encode characters in position 24-25: ordinal not in range(128)
Interestingly, this error does not occur when running directly in the Python environment. Uwsgi. ini is also running normally.
Because unicode is not fully supported, such errors are often reported in Python2, but all of my programs are written in Python3, and such errors should not occur again. Besides, all python files are encoded in the first line:
# -*- coding: utf-8 -*-
My environment is as follows:
The uwsgi. ini configuration file is as follows:
[uwsgi]master = truewsgi-file = manage.pycallable = appprocesses = 2threads = 2max-requests = 6000chmod-socket = 664uid = appgid = appbuffer-size = 32768venv = {project_dir}/venv; http = 127.0.0.1:5001logto = {project_dir}/logs/uwsgi.log
Because Python and uwsgi are used directly without such errors, you can determine whether it is caused by the environment encoding settings.
Check the server encoding as follows:
% localeLANG=CLANGUAGE=C:LC_CTYPE="en_US.UTF-8"LC_NUMERIC="en_US.UTF-8"LC_TIME="en_US.UTF-8"LC_COLLATE="en_US.UTF-8"LC_MONETARY="en_US.UTF-8"LC_MESSAGES="en_US.UTF-8"LC_PAPER="en_US.UTF-8"LC_NAME="en_US.UTF-8"LC_ADDRESS="en_US.UTF-8"LC_TELEPHONE="en_US.UTF-8"LC_MEASUREMENT="en_US.UTF-8"LC_IDENTIFICATION="en_US.UTF-8"LC_ALL=en_US.UTF-8
The LANG and LANGUAGE environment variables are not set.
You can set the values of these two environment variables in uwsgi. ini. After testing, we found that the LANGUAGE actually works.
env LANG="en_US.UTF-8"env LANGUAGE="en_US.UTF-8"
The above is a detailed description of the uWSGI Encoding Solution. For more information, see other related articles in the first PHP community!