Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mod#u cgi+;utf8+;Python3不产生任何输出_Python_Python 3.x_Apache_Utf 8_Cgi - Fatal编程技术网

mod#u cgi+;utf8+;Python3不产生任何输出

mod#u cgi+;utf8+;Python3不产生任何输出,python,python-3.x,apache,utf-8,cgi,Python,Python 3.x,Apache,Utf 8,Cgi,如果您创建此app.py #!/usr/bin/python3 print("Content-Type: text/plain;charset=utf-8\n") print("Hello World!") 您可以在.htaccess中启用CGI,方法是: Options +ExecCGI AddHandler cgi-script .py 它完全有效:显示“Hello world!” 但是,如果添加重音字符: print("Quel été!") 这不再有效:浏览器中的输出页面为空 问题

如果您创建此
app.py

#!/usr/bin/python3
print("Content-Type: text/plain;charset=utf-8\n")
print("Hello World!")
您可以在
.htaccess
中启用CGI,方法是:

Options +ExecCGI
AddHandler cgi-script .py
它完全有效:显示“Hello world!”

但是,如果添加重音字符:

print("Quel été!")
这不再有效:浏览器中的输出页面为空

问题:如何使用Python3+
mod\u cgi输出UTF8内容?

注意:

  • .py文件以UTF8编码保存

  • 可能与:

  • 有趣的事实:跑步

    #!/usr/bin/python3
    import sys
    print("Content-Type: text/plain;charset=utf-8\n")
    print(str(sys.stdout.encoding))
    
    从命令行发出
    UTF-8
    ,但通过
    mod_cgi
    输出
    ANSI_X3.4-1968
    in运行它

    设置
    export pythonionecoding=utf8
    没有改变任何东西,可能是因为调用此Python代码的是Apache+mod_cgi

简易解决方案 只需添加:

SetEnv PYTHONIOENCODING utf8
.htaccess
中,以及:

Options +ExecCGI
AddHandler cgi-script .py
另见和。也相关,但没有直接解决问题的答案:

替代溶液 对于Python 3-3.6(请参阅):

对于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 encoded.
import sys
sys.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-8\n")
print(str(sys.stdout.encoding))  # I still get: ANSI_X3.4-1968
容易解决 只需添加:

SetEnv PYTHONIOENCODING utf8
.htaccess
中,以及:

Options +ExecCGI
AddHandler cgi-script .py
另见和。也相关,但没有直接解决问题的答案:

替代溶液 对于Python 3-3.6(请参阅):

对于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 encoded.
import sys
sys.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-8\n")
print(str(sys.stdout.encoding))  # I still get: ANSI_X3.4-1968

可能有一个(编码相关的)异常没有显示在浏览器中(您可能会看到一个错误500,但我从未使用过Python CGI,所以我不知道)。在Python解释器启动时,设置STD流的编码涉及到一些基于环境的启发式方法;看见在*nix平台上,您可能可以通过
pythonoencoding
env变量解决此问题。@lenz我刚刚做了一些测试,编辑了问题(参见最后一点);在bash中执行
export-pythonionecoding=utf8
似乎没有改变任何事情。你知道吗?可能有一个(编码相关的)异常没有显示在浏览器中(你可能会看到一个错误500,但我从来没有使用过Python CGI,所以我不知道)。在Python解释器启动时,设置STD流的编码涉及到一些基于环境的启发式方法;看见在*nix平台上,您可能可以通过
pythonoencoding
env变量解决此问题。@lenz我刚刚做了一些测试,编辑了问题(参见最后一点);在bash中执行
export-pythonionecoding=utf8
似乎没有改变任何事情。有什么想法吗?