I need to understand if this is a bug or if it is the expected behaviour and I'm doing something wrong.
I have two web2py applications. One of them implements an XMLRPC webservice, and the other one consumes it.
All the controllers (in both applications) have this first line:
One of the applications connects to the webservice to call a specific function, and it needs to pass some string arguments:
def make_the_call():
from xmlrpclib import ServerProxy
service = ServerProxy(webservice_url)
data = {
'title': 'áéíóú',
'detail': 'Detail with special characters like ñ or Ç'
}
service.add_content(data)
The application that implements the webservice:
from gluon.tools import Service
service = Service()
def call():
return service()
def add_content(data):
db.content.insert(
title=data.get('title'),
detail=data.get('detail')
)
return {'success': True}
But the sentence service.add_content(data) fails with this error and traceback:
Traceback (most recent call last):
File "/home/gonguinguen/medios/gluon/restricted.py", line 219, in restricted
exec(ccode, environment)
File "/home/gonguinguen/medios/applications/webmedios/controllers/admin.py", line 707, in <module>
File "/home/gonguinguen/medios/gluon/globals.py", line 421, in <lambda>
self._caller = lambda f: f()
File "/home/gonguinguen/medios/applications/webmedios/controllers/admin.py", line 704, in test
admin_password_sitio='93c824d1-91c4-428f-8542-db5db9d4594b')
File "/home/gonguinguen/medios/applications/webmedios/controllers/admin.py", line 695, in instalar_demo_contenido
r = server.add_content(data)
File "/usr/lib64/python2.7/xmlrpclib.py", line 1233, in __call__
return self.__send(self.__name, args)
File "/usr/lib64/python2.7/xmlrpclib.py", line 1591, in __request
verbose=self.__verbose
File "/usr/lib64/python2.7/xmlrpclib.py", line 1273, in request
return self.single_request(host, handler, request_body, verbose)
File "/usr/lib64/python2.7/xmlrpclib.py", line 1306, in single_request
return self.parse_response(response)
File "/usr/lib64/python2.7/xmlrpclib.py", line 1482, in parse_response
return u.close()
File "/usr/lib64/python2.7/xmlrpclib.py", line 794, in close
raise Fault(**self._stack[0])
Fault: <Fault 1: "<type 'exceptions.UnicodeEncodeError'>:'ascii' codec can't encode character u'\\xe1' in position 1: ordinal not in range(128)">
To make it work, I need to reload sys and set default encoding in the add_content() method of the webservice:
def add_content(data):
import sys
reload(sys)
sys.setdefaultencoding('utf8')
db.content.insert(
title=data.get('title'),
detail=data.get('detail')
)
return {'success': True}
After that, it works ok.
But here is the weird part: after having made one successfull call to the webservice method, I can remove the lines where I reload the sys, and it keeps working ok. But if I reload uwsgi, it starts throwing error again.
Anyway, I've read that reloading sys is not a good practise at all. So I'm pretty lost. I presumed that I could add "# -*- coding: utf-8 -*-" and forget about the encoding problems.
What should I do?