簡體   English   中英

SUDS中的UnicodeDecodeError,但僅在py2exe生成的.exe中

[英]UnicodeDecodeError in SUDS but only in the .exe generated by py2exe

我有一個Python 2.7腳本SendPreord.py ,它使用SUDS與Web服務通信。 在腳本中,我調用一個Web服務方法,將一些參數作為字符串傳遞( runJob(par1, par2, par3) )。 它可以與弦樂中的西歐字符配合使用。 我使用PyDev在Eclipse中運行它。

然后我使用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

您對Python的編碼轉換猜測感到困惑。 您嘗試的第一部分是正確的:首先使用(希望正確)編碼進行解碼。 在發送之前,您必須再次對其進行編碼,最好使用UTF-8之類的格式,否則Python會嘗試“默認”編碼(在大多數安裝中為ASCII)。 我以前在這里寫過

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM