Python 3.4在Apache2服务器(Mac)上导致UnicodeEncodeError,但在命令行中工作正常 [英] Python 3.4 causes UnicodeEncodeError on Apache2 server (Mac) but works fine in command line

查看:101
本文介绍了Python 3.4在Apache2服务器(Mac)上导致UnicodeEncodeError,但在命令行中工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取Python 3.4 cgi脚本和Apache以在浏览器中输出'ü'字符(就此而言,任何其他Unicode字符都会发生相同的问题). python 3.4 cgi脚本在Apache中导致UnicodeEncodeError,而类似的python 2.7代码在同一服务器上也可以正常工作.脚本3.4和2.7均可从命令行正常运行.

I'm trying to get a Python 3.4 cgi script and Apache to output an 'ü' character in the browser (same problem occurs for any other Unicode character, for that matter). The python 3.4 cgi script causes a UnicodeEncodeError in Apache while a similar python 2.7 code works fine on the same server. Both scripts 3.4 and 2.7 work fine from the command line.

这是我在运行python 3.4脚本时遇到的错误:

This is the error that I get while running the python 3.4 script:

UnicodeEncodeError:'ascii'编解码器无法在位置23编码字符'\ xfc':序数不在范围内(128)

UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 23: ordinal not in range(128)

以下是导致该错误的代码:

Here's the code that causes that error:

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

print ("Content-Type: text/html; charset=utf-8\n\n")
print ("""\
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
""")

print ("U umlaut (Python 3.4): ü<br>")

print ("""\
</body>
</html>
""")

同一台服务器上的以下Python 2.7脚本正确显示ü和其他任何Unicode字符: (所以这不是Apache的问题吗?)

The Python 2.7 script below on the same server displays ü and any other Unicode characters correctly: (so it's not an Apache problem?)

#!/usr/bin/python
# -*- coding: utf-8 -*-

print "Content-Type: text/html; charset=utf-8\n\n"
print """\
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
"""

print "U umlaut (Python 2.7): ü<br>"

print """\
</body>
</html>
"""

两个脚本都可以从命令行正常工作.我已经有

Both scripts work correctly from the command line. I already have

AddDefaultCharset UTF-8

在我的httpd.conf中.

in my httpd.conf.

此外,我的语言环境变量设置如下:

Also, my locale variables are set as follows:

LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

一个我已经想到的地方(有时过分)已经包含了UTF-8设置.有谁知道我还能做什么才能使python 3.4脚本在浏览器中正确显示Unicode字符?谢谢.

A already included the UTF-8 setting everywhere I could think of (sometimes excessively). Does anyone know what else I can do to get the python 3.4 script display Unicode characters correctly in the browser? Thanks.

推荐答案

自您提出问题以来,我知道这已经是几个月了,但我偶然遇到了同样的问题.我找到了解决方案.也许对其他搜寻者没有帮助.

I know it is a few moons since your question but i stumbled over it facing the same problem. And I found a solution. Maybe it will not help you but other seekers.

Jack O'Connor的解决方案解决了对我来说有问题,只需尝试以下操作即可:

Jack O'Connor's solution fixed the problem for me just try this:

import sys
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
print("日本語")
# Also works with other methods of writing to stdout:
sys.stdout.write("日本語\n")
sys.stdout.buffer.write("日本語\n".encode())`

这篇关于Python 3.4在Apache2服务器(Mac)上导致UnicodeEncodeError,但在命令行中工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆