在py2exe生成的.exe中出现SUDS的UnicodeDecodeError

2 投票
1 回答
836 浏览
提问于 2025-04-17 01:48

我有一个Python 2.7的脚本SendPreord.py,它通过SUDS和网络服务进行通信。在这个脚本中,我调用一个网络服务的方法,传递一些参数作为字符串runJob(par1, par2, par3))。这个方法在处理西欧字符时运行得很好。我是在Eclipse中用PyDev运行这个脚本的。

然后我使用py2exe生成了一个.exe文件。现在它给我报错了:

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)

导致错误的代码是:

result = ws_client.service.runJob(par1, par2, par3)

经过调查,我发现去掉像°èòà这样的字符可以解决问题……但是我不能这样做!我必须保留我传递的字符串。

所以我尝试在传递字符串之前解码它们:

result = ws_client.service.runJob(par1.decode('latin9'), par2.decode('latin9'), par3.decode('latin9'))

结果是在.py文件中一切正常,但在.exe中却不行。也许PyDev以某种方式修复了这个问题?


附件

Setup.py:

from distutils.core import setup
import py2exe
setup(console=['src/SendPreord.py'])

来自py2exe输出日志的有趣摘录:

*** 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

1 个回答

0

你遇到了Python在编码转换时的麻烦。你最开始的做法是对的:先用(希望是正确的)编码进行解码。在你发送数据之前,你需要再编码一次,最好使用像UTF-8这样的编码方式,否则Python会尝试使用“默认”编码(大多数情况下是ASCII)。我之前在这里写过这个

撰写回答