UnicodeDecodeError in SUDS but only in the .exe generated by py2exe
I have a Python 2.7 script SendPreord.py which communicates with web services using SUDS. In the script I call a web service method passing some parameters as strings (runJob(par1, par2, par3)
). It works well with western european characters in the strings. I run it in Eclipse with PyDev.
Then I generate the .exe using py2exe. Now it gives me the error
Traceback (most recent call last):
File "SendPreord.py", line 80, in <module>
File "suds\client.pyc", line 542, in __call__
File "suds\client.pyc", line 602, in invoke
File "suds\client.pyc", line 637, in send
File "suds\transport\https.pyc", line 64, in send
File "suds\transport\http.pyc", line 77, in send
File "suds\transport\http.pyc", line 118, in u2open
File "urllib2.pyc", line 391, in open
File "urllib2开发者_如何学运维.pyc", line 409, in _open
File "urllib2.pyc", line 369, in _call_chain
File "urllib2.pyc", line 1173, in http_open
File "urllib2.pyc", line 1142, in do_open
File "httplib.pyc", line 946, in request
File "httplib.pyc", line 987, in _send_request
File "httplib.pyc", line 940, in endheaders
File "httplib.pyc", line 801, in _send_output
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 484: ordinal not in range(128)
The code causing the error is:
result = ws_client.service.runJob(par1, par2, par3)
Investigating, I realized that removing characters like °èòà
solves the problem... but I can't! I have to preserve the strings I'm passing.
So I tried decoding strings before passing them:
result = ws_client.service.runJob(par1.decode('latin9'), par2.decode('latin9'), par3.decode('latin9'))
Again all works in .py, but not in .exe. Maybe PyDev corrects the issue in some way?
ATTACHMENTS
Setup.py:
from distutils.core import setup
import py2exe
setup(console=['src/SendPreord.py'])
Interesting extracts from py2exe output log:
*** copy dlls ***
copying C:\Python27\lib\site-packages\py2exe\run.exe -> C:\Users\xxxxxxx\workspace\eclipse\SendPreord\dist\SendPreord.exe
The following modules appear to be missing
['ElementC14N', '_scproxy', 'ntlm']
*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.
Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.
USER32.dll - C:\Windows\system32\USER32.dll
SHELL32.dll - C:\Windows\system32\SHELL32.dll
WSOCK32.dll - C:\Windows\system32\WSOCK32.dll
ADVAPI32.dll - C:\Windows\system32\ADVAPI32.dll
WS2_32.dll - C:\Windows\system32\WS2_32.dll
KERNEL32.dll - C:\Windows\system32\KERNEL32.dll
You've been bitten by Python's guessing on encoding conversions. The first part you tried is right: decode first with the (hopefully correct) encoding. Before you send it out, you have to encode it again, preferrably using something like UTF-8, or else Python tries the "default" encoding (which is in most installations ASCII). I've written this here before
精彩评论