Python 打印在不同服务器上的工作方式不同

发布于 2024-09-19 13:23:33 字数 513 浏览 5 评论 0原文

当我尝试在开发服务器上打印 unicode 字符串时,它可以正常工作,但生产服务器会引发异常。

File "/home/user/twistedapp/server.py", line 97, in stringReceived
    print "sent:" + json
File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 555, in write
    d = (self.buf + data).split('\n')
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 28: ordinal not in range(128)

实际上它是扭曲的应用程序并转发到日志文件。

字符串的 repr() 是相同的。区域设置设置为 en_US.UTF-8。

我需要检查任何配置才能使其在两台服务器上同样工作吗?

When I try to print an unicode string on my dev server it works correctly but production server raises exception.

File "/home/user/twistedapp/server.py", line 97, in stringReceived
    print "sent:" + json
File "/usr/lib/python2.6/dist-packages/twisted/python/log.py", line 555, in write
    d = (self.buf + data).split('\n')
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 28: ordinal not in range(128)

Actually it is twisted application and print forwards to log file.

repr() of strings are the same. Locale set to en_US.UTF-8.

Are there any configs I need to check to make it work the same on the both servers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万人眼中万个我 2024-09-26 13:23:33

Unicode 字符串的打印依赖于 sys.stdout(进程的标准输出),该输出具有正确的 .encoding 属性,Python 可以使用该属性对字符串进行编码unicode 字符串转换为字节字符串以执行所需的打印 - 该设置取决于操作系统的设置方式、标准输出定向到的位置等等。

如果没有这样的属性,则使用默认编码的 ascii,并且正如您所见,它通常不会提供所需的结果;-)。

您可以检查 getattr(sys.stdout, 'encoding', None) 来查看编码是否存在(如果是,您可以祈祷它是正确的......或者,也许,尝试一些特定于平台的技巧来猜测要检查的正确系统编码;-)。如果不是,一般来说,没有可靠的或跨平台的方法来猜测它可能是什么。您可以尝试'utf8',这是一种在很多情况下都有效的通用编码(肯定比ascii更有效;-),但它是真的是轮盘赌的旋转。

为了提高可靠性,您的程序应该有自己的配置文件来告诉它使用什么输出编码(如果没有另外指定,可能使用 'utf8' 作为默认值)。

为了可移植性,执行您自己的编码也更好,也就是说,不是

print someunicode

而是

print someunicode.encode(thecodec)

实际上,如果您宁愿有不完整的输出而不是崩溃

print someunicode.encode(thecodec, 'ignore')

(这只是跳过不可编码的字符) ,或者,通常更好,

print someunicode.encode(thecodec, 'replace')

(对不可编码的字符使用问号占位符)。

printing of Unicode strings relies on sys.stdout (the process's standard output) having a correct .encoding attribute that Python can use to encode the unicode string into a byte string to perform the required printing -- and that setting depends on the way the OS is set up, where standard output is directed to, and so forth.

If there's no such attribute, the default coded ascii is used, and, as you've seen, it often does not provide the desired results;-).

You can check getattr(sys.stdout, 'encoding', None) to see if the encoding is there (if it is, you can just keep your fingers crossed that it's correct... or, maybe, try some heavily platform-specific trick to guess at the correct system encoding to check;-). If it isn't, in general, there's no reliable or cross-platform way to guess what it could be. You could try 'utf8', the universal encoding that works in a lot of cases (surely more than ascii does;-), but it's really a spin of the roulette wheel.

For more reliability, your program should have its own configuration file to tell it what output encoding to use (maybe with 'utf8' just as the default if not otherwise specified).

It's also better, for portability, to perform your own encoding, that is, not

print someunicode

but rather

print someunicode.encode(thecodec)

and actually, if you'd rather have incomplete output than a crash,

print someunicode.encode(thecodec, 'ignore')

(which simply skips non-encodable characters), or, usually better,

print someunicode.encode(thecodec, 'replace')

(which uses question-mark placeholders for non-encodable characters).

谜兔 2024-09-26 13:23:33

Twisted 的内置日志观察器不支持 Unicode。请参阅 http://twistedmatrix.com/trac/ticket/989 了解添加对或者看看你能提供什么帮助。

在 #989 得到解决并且您的应用程序部署在 Twisted 版本中得到修复之前,请勿记录 unicode。仅记录str

Unicode is not supported by Twisted's built-in log observers. See http://twistedmatrix.com/trac/ticket/989 for progress on adding support for this, or to see what you can do to help out.

Until #989 is resolved and the fix is in a Twisted release your application is deployed on, do not log unicode. Only log str.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文