How to solve uWSGI encoding problems

Source: Internet
Author: User
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:

  • Ubuntu 16.04.1 LTS

  • Python 3.5.2

  • UWSGI 2.0.14 (in python3 pip)

  • Supervisor 3.3.1 (in python2 pip)

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.