mod_cgi + utf8 + Python3不产生任何输出

mod_cgi + utf8 + Python3不产生任何输出,第1张

mod_cgi + utf8 + Python3不产生任何输出 简单的解决方案

只需添加:

SetEnv PYTHonIOENCODING utf8

在中

.htaccess
,以及:

Options +ExecCGIAddHandler cgi-script .py

另请参见使用apache服务器和Problèmepython apache et
utf8
时覆盖python3默认编码器。也相关但没有答案可以直接解决:在Python 3
CGI脚本中设置编码。

替代解决方案

对于Python 3-3.6(请参阅如何在Python
3中设置sys.stdout编码?):

import sys, precssys.stdout = precs.getwriter("utf-8")(sys.stdout.detach())

对于Python 3.7+:

sys.stdout.reconfigure(encoding='utf-8')

注意:即使有时在某些答案中引用它,以下脚本 也不适用 于Apache 2.4 + mod_cgi + Python3:

import locale # Ensures that subsequent open()s locale.getpreferredencoding = lambda: 'UTF-8'  # are UTF-8 enpred.import syssys.stdin = open('/dev/stdin', 'r') # Re-open standard files in UTF-8 sys.stdout = open('/dev/stdout', 'w')          # mode.sys.stderr = open('/dev/stderr', 'w') print("Content-Type: text/plain;charset=utf-8n")print(str(sys.stdout.encoding))  # I still get: ANSI_X3.4-1968


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5663442.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存