我想用 python 3 尝试 Django。
我用:
- Nginx
- 来自 Ubuntu repos 的 uWSGI + python 3 插件
- 蟒蛇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 的错误。
更新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