简体   繁体   English

在 Apache 中的 Flask 上编码运行 Python 文件的问题

[英]Problem with encoding running Python file on flask in Apache

Using Flask on apache webserver on CentOS (ClearOS).在 CentOS (ClearOS) 上的 apache 网络服务器上使用 Flask。

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.运行一个 APP FLASK web 项目并在该文件中我想使用命令运行另一个 python 文件: os.system("python3 module.py") 这成为一个问题,因为它返回编码 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.在我的应用程序中,我想将文件作为 cron 运行,所以我将打开文件并按如下方式运行。

    @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.在我使用 Flask 的本地环境中,这有效并返回 utf-8。

In the file "module.py" I use command: sys.stdout.encoding and that is what's giving me the results.在文件“module.py”中,我使用命令:sys.stdout.encoding 这就是给我结果的原因。

I'm using the same python3.8 and same env.我使用相同的 python3.8 和相同的环境。 So the difference is different OS and Apache instead of Werkzeug.所以区别在于不同的操作系统和 Apache 而不是 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.默认情况下,Apache 不一定使用系统区域设置。

Try uncommenting the last line in /etc/apache2/envvars and restarting Apache.尝试取消注释 /etc/apache2/envvars 中的最后一行并重新启动 Apache。 This will make Apache use your system locale.这将使 Apache 使用您的系统区域设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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