vendredi 14 novembre 2014

Django + mod_wsgi + apache dans CentOS a UnicodeDecodeError - Stack Overflow


Every time when i restart apache manually (by sudo /etc/init.d/httpd restart)


My project work fine for a while.


But a few days later, it will has 500 error when my code try to use glob.iglob like this


Traceback (most recent call last):
File "/www/django/virtualenv/gplab/lib/python2.6/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/www/django/gplab/gplab/views.py", line 166, in charging_content
for file in glob.iglob(os.path.join(target_dir_path, '*')) :
File "/usr/lib/python2.6/glob.py", line 43, in iglob
yield os.path.join(dirname, name)
File "/usr/lib/python2.6/posixpath.py", line 70, in join
path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 19: ordinal not in range(128)

I've tried to auto restart apache by crontab, and it don't work.


Then, i check sys default encoding by sys.getdefaultencoding()


I get ascii


But i've set LANG and LC_ALL to zh_TW.UTF-8 in /etc/profile, and i tried to set HTTPD_LANG to zh_TW.UTF-8 in /etc/sysconfig/httpd


It don't work.


I get some value As follows


locale.getlocale()   # (None, None)
locale.getdefaultlocale() # ('zh_TW', 'UTF8')
sys.getfilesystemencoding() # UTF-8
sys.getdefaultencoding() # ascii

When I use sudo /etc/init.d/httpd force-reload, it become


locale.getlocale()   # ('zh_TW', 'UTF8')
locale.getdefaultlocale() # ('zh_TW', 'UTF8')
sys.getfilesystemencoding() # UTF-8
sys.getdefaultencoding() # ascii

It seems that set HTTPD_LANG to zh_TW.UTF-8 in /etc/sysconfig/httpd does't chenge sys default encoding under apache with mod_wsgi.


How can I fix that?




Did you read the following part of the official Django documentation.




Every time when i restart apache manually (by sudo /etc/init.d/httpd restart)


My project work fine for a while.


But a few days later, it will has 500 error when my code try to use glob.iglob like this


Traceback (most recent call last):
File "/www/django/virtualenv/gplab/lib/python2.6/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/www/django/gplab/gplab/views.py", line 166, in charging_content
for file in glob.iglob(os.path.join(target_dir_path, '*')) :
File "/usr/lib/python2.6/glob.py", line 43, in iglob
yield os.path.join(dirname, name)
File "/usr/lib/python2.6/posixpath.py", line 70, in join
path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 19: ordinal not in range(128)

I've tried to auto restart apache by crontab, and it don't work.


Then, i check sys default encoding by sys.getdefaultencoding()


I get ascii


But i've set LANG and LC_ALL to zh_TW.UTF-8 in /etc/profile, and i tried to set HTTPD_LANG to zh_TW.UTF-8 in /etc/sysconfig/httpd


It don't work.


I get some value As follows


locale.getlocale()   # (None, None)
locale.getdefaultlocale() # ('zh_TW', 'UTF8')
sys.getfilesystemencoding() # UTF-8
sys.getdefaultencoding() # ascii

When I use sudo /etc/init.d/httpd force-reload, it become


locale.getlocale()   # ('zh_TW', 'UTF8')
locale.getdefaultlocale() # ('zh_TW', 'UTF8')
sys.getfilesystemencoding() # UTF-8
sys.getdefaultencoding() # ascii

It seems that set HTTPD_LANG to zh_TW.UTF-8 in /etc/sysconfig/httpd does't chenge sys default encoding under apache with mod_wsgi.


How can I fix that?



Did you read the following part of the official Django documentation.



0 commentaires:

Enregistrer un commentaire