【问题标题】:UnicodeError when try to send a file with greek filename尝试发送带有希腊文件名的文件时出现 UnicodeError
【发布时间】:2018-09-24 16:46:28
【问题描述】:

我在 set() 中创建并填充了希腊名称,然后将这组值传递给视图函数。

当我尝试打印这组希腊名字时,会出现乱码。我相信这与 Apache mod_wsgi 或 Bottle 不支持 utf-8 开始有关。

我如何告诉 Apache/Bottle 使用 LANG=el_GR.utf-8 以便我可以正确显示 unicode,因为我相信这里就是这种情况?

我在 httpd.conf 中寻找 AddDefaultCharset utf-8 但它已经启用,所以我不得不问为什么希腊字符显示为乱码?

这是当我尝试下载具有希腊文件名的文件时。

Error: 500 Internal Server Error
Sorry, the requested URL 'http://superhost.gr/downloads/file' caused an error:

Internal Server Error
Exception:
UnicodeEncodeError('ascii', '/static/files/Î\x92ιογÏ\x81αÏ\x86ικÏ\x8c - Î\x9dίκοÏ\x82.docx', 14, 34, 'ordinal not in range(128)')
Traceback:
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/usr/lib/python3.6/site-packages/bottle.py", line 1740, in wrapper
    rv = callback(*a, **ka)
  File "/usr/lib/python3.6/site-packages/bottle.py", line 2690, in wrapper
    return func(*a, **ka)
  File "/home/nikos/public_html/downloads.py", line 148, in file
    return static_file(filename, root='/static/files', download=True)
  File "/usr/lib/python3.6/site-packages/bottle.py", line 2471, in static_file
    if not os.path.exists(filename) or not os.path.isfile(filename):
  File "/usr/lib64/python3.6/genericpath.py", line 19, in exists
    os.stat(path)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-33: ordinal not in range(128)

用于下载文件的代码是:

return static_file(filename, root='/static/files', download=True)

我的系统是 utf-8 格式

[root@superhost public_html]# echo $LANG
en_US.UTF-8

也许是 Apache 的问题,还是 Python3 的问题?

【问题讨论】:

    标签: python apache


    【解决方案1】:

    您不能使用带有 unicode 文件名和 download=True 的 Bottle static_file()。有关此限制的两种替代解决方案,请参阅this question 的已接受答案。

    【讨论】:

    • 所以,我必须省略下载?那么带有希腊文件名的文件呢,我如何在没有 Bottle 的情况下将这些文件提供给用户返回错误 `UnicodeEncodeError('ascii', '/static/files/Î\x92ιÎ我看到了你给我的链接但不明白 muhc。如何我应该写回报吗?
    • 您可以尝试使用希腊文件名“return static_file(filename, root='/static/files')”,该解决方案适用于原始问题的作者。或者您可以尝试“return static_file(filename, root='/static/files', download='/some/english/filename')”发送带有新英文文件名的本地希腊文件。
    • 我确实为拉丁文件名工作,但不适用于希腊文件名,当我尝试下载它们时,我收到错误 ``UnicodeEncodeError('ascii', '/static/files/Î\x92ιÎI `
    • 然后你可以尝试第二个变种 - “return static_file(filename, root='/static/files', download='some_english_filename')”,它应该使用希腊文件名的本地文件并发送它以英文文件名发送给客户。
    • 每次我尝试 yse download=.... 时,它都会给我错误 file does nto exists
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多