简体   繁体   中英

Problem with encoding running Python file on flask in Apache

Using Flask on apache webserver on CentOS (ClearOS).

Running an APP FLASK web project and in that file I want to run another python file with command: os.system("python3 module.py") which becomes a problem because it returns encoding ANSI_X3.4-1968.

In my app I would like to run the file as a cron so I will just open the file and run as below.

    @app.route('/crontab')
    def do_crontab():
        crontab_arg = request.args.get('cron')
        app_root = os.path.dirname(os.path.abspath(__file__))
        crontab_run = "python3 " + app_root + "/module.py " + crontab_arg
        os.system(crontab_run)
        return "Done"

At my local environment with Flask this works and returns utf-8.

In the file "module.py" I use command: sys.stdout.encoding and that is what's giving me the results.

I'm using the same python3.8 and same env. So the difference is different OS and Apache instead of Werkzeug.

On my server:


    [root@server ~]# locale
    LANG=sv_SE.UTF-8
    LC_CTYPE="sv_SE.UTF-8"
    LC_NUMERIC="sv_SE.UTF-8"
    LC_TIME="sv_SE.UTF-8"
    LC_COLLATE="sv_SE.UTF-8"
    LC_MONETARY="sv_SE.UTF-8"
    LC_MESSAGES="sv_SE.UTF-8"
    LC_PAPER="sv_SE.UTF-8"
    LC_NAME="sv_SE.UTF-8"
    LC_ADDRESS="sv_SE.UTF-8"
    LC_TELEPHONE="sv_SE.UTF-8"
    LC_MEASUREMENT="sv_SE.UTF-8"
    LC_IDENTIFICATION="sv_SE.UTF-8"
    LC_ALL=sv_SE.UTF-8

Anyone has any idea?

Apache doesn't necessarily use the system locale by default.

Try uncommenting the last line in /etc/apache2/envvars and restarting Apache. This will make Apache use your system locale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM