【问题标题】:X-Accel-Redirect doesn't work with python3 and Django 1.5 with non ascii filenamesX-Accel-Redirect 不适用于带有非 ascii 文件名的 python3 和 Django 1.5
【发布时间】:2013-03-28 17:29:13
【问题描述】:

我想用 python 3 尝试 Django。

我使用:

  • Nginx
  • 来自 Ubuntu 存储库的 uWSGI + python 3 插件
  • python 3.2
  • 虚拟环境

当我为 python 2.7 制作它时,我有这个并且它有效:

...
fname_ascii = fname.encode('utf-8')
response = HttpResponse()
response['X-Accel-Redirect'] = _convert_file_to_url(fname_ascii)

现在使用 python 3 我将 1 行替换为:

response['X-Accel-Redirect'] = bytes(_convert_file_to_url(fname), 'utf-8')

这导致:

Sat Apr  6 11:27:30 2013 - !!! uWSGI process 6296 got Segmentation Fault !!!
Sat Apr  6 11:27:30 2013 - *** backtrace of 6296 ***
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(uwsgi_backtrace+0x25) [0x430a55]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(uwsgi_segfault+0x21) [0x430b31]
Sat Apr  6 11:27:30 2013 - /lib/x86_64-linux-gnu/libc.so.6(+0x364a0) [0x7f2177f544a0]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyBytes_AsString+0) [0x7f217417e280]
Sat Apr  6 11:27:30 2013 - /usr/lib/uwsgi/plugins/python32_plugin.so(py_uwsgi_spit+0x45f) [0x7f217455c5ef]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyEval_EvalFrameEx+0x6230) [0x7f217405f310]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyEval_EvalCodeEx+0x687) [0x7f21741289e7]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(+0x12acff) [0x7f2174128cff]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyObject_Call+0x7a) [0x7f217413f8aa]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(+0xac98d) [0x7f21740aa98d]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyObject_Call+0x7a) [0x7f217413f8aa]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(+0xc773c) [0x7f21740c573c]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyObject_Call+0x7a) [0x7f217413f8aa]
Sat Apr  6 11:27:30 2013 - /usr/lib/libpython3.2mu.so.1.0(PyEval_CallObjectWithKeywords+0x47) [0x7f2174180cb7]
Sat Apr  6 11:27:30 2013 - /usr/lib/uwsgi/plugins/python32_plugin.so(python_call+0x24) [0x7f21745592f4]
Sat Apr  6 11:27:30 2013 - /usr/lib/uwsgi/plugins/python32_plugin.so(uwsgi_request_wsgi+0x119) [0x7f217455b6c9]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(wsgi_req_recv+0x7f) [0x40dd5f]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(simple_loop+0x112) [0x42be12]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(uwsgi_ignition+0x18a) [0x430faa]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(uwsgi_start+0x2c36) [0x434586]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi(main+0xed4) [0x40d6e4]
Sat Apr  6 11:27:30 2013 - /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f2177f3f76d]
Sat Apr  6 11:27:30 2013 - /usr/bin/uwsgi() [0x40d781]
Sat Apr  6 11:27:30 2013 - *** end of backtrace ***
Sat Apr  6 11:27:30 2013 - DAMN ! worker 2 (pid: 6296) died :( trying respawn ...
Sat Apr  6 11:27:30 2013 - Respawned uWSGI worker 2 (new pid: 6299)

我也试过了:

response['X-Accel-Redirect'] = _convert_file_to_url(fname)

但是得到了:

BadHeaderError at /my_url

Header values can't contain newlines (got '=?utf-8?b?...=?=\n =?utf-8....=?=\n =?utf-8?b?...=?=')

uWSGI 设置已更改:

[uwsgi]
...
plugins = python32
virtualenv = /pyenvs/p3_general/

如果有人遇到这个问题 - 写下你知道的。
或者也许它只是一个错误。不过好像是uwsgi-plugin的bug。

更新 1:

我使用 pip 安装了 uwsgi 1.9.5:

Starting uWSGI 1.9.5 (64bit) on [Sat Apr  6 16:27:03 2013] 
...
Python version: 3.2.3 (default, Oct 19 2012, 19:55:38)  [GCC 4.7.2]

同样的问题。

我忘了提 - 这仅发生在具有非 ascii 符号的文件名(在我的情况下为俄语)

更新 2:

使用urllib.parse.quote uWSGI 可以工作,但 Nginx 会报错:

  • failed (36: File name too long)(轻松修复)
  • open() "/data/SORT/%D1%84%D1%8B%D0%B2%D0%B0%20%D1%84%D1%8B.ogg" failed (2: No such file or directory)

所以我认为的问题是 - uWSGI + python 3

【问题讨论】:

    标签: django python-3.x uwsgi


    【解决方案1】:

    我强烈建议您使用 uWSGI 稳定版本(1.4 或 1.9 树)。 debian 提供的版本非常旧,并且有各种已知的错误

    【讨论】:

    • 我刚刚安装了 1.9.5 - 同样的问题
    • 你能仔细检查一下你是否真的在使用 1.9.5(它在启动日志中报告)。我无法在最新版本中重现您的问题
    • 是的,1.9.5。我忘了提 - 这仅发生在具有非 ascii 符号的文件名(在我的情况下为俄语)
    • 嗯,这是一个有趣的问题。标头(在 pep3333 中)应该被强制为 latin1,我想为俄罗斯字符这样做是不可能的(段错误很可能是由错误/未完成的转换产生的)。在生成标头之前对文件名进行 url_encoding 怎么样?不知道对nginx好不好
    • Used urllib.parse.quote_plus(myurl, '/') - uwsgi 没有崩溃,但 Nginx 给出了 404(将朝这个方向挖掘)。有趣的部分是它适用于 python 2.7 - 我只是将 unicode 文件名解码为 ascii 字符串,现在我尝试了bytes(),但它总是失败。
    猜你喜欢
    • 2023-03-04
    • 2018-02-02
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多