TypeError:initial_value必须是unicode或None,而不是str,

Adi*_*lik 6 python unicode soap soappy python-2.7

我正在使用SOAPpy进行soap wsdl服务.我跟着这个toturail.我的代码如下

from SOAPpy import WSDL
wsdlfile = 'http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL'
server = WSDL.Proxy(wsdlfile)
Run Code Online (Sandbox Code Playgroud)

我在代码的最后一行收到此错误

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/SOAPpy/WSDL.py", line 85, in __init__
self.wsdl = reader.loadFromString(str(wsdlsource))
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/wstools/WSDLTools.py", line 52, in loadFromString
return self.loadFromStream(StringIO(data))
TypeError: initial_value must be unicode or None, not str
Run Code Online (Sandbox Code Playgroud)

我尝试将字符串转换为utf使用

wsdlFile = unicode('http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL, "utf-8")
Run Code Online (Sandbox Code Playgroud)

但仍然有同样的错误.这里缺少什么?

小智 1

我刚刚遇到了这个问题,一些非常旧的 2.7 代码由于 TLS 更新而不再工作。更新到最新版本的 Python 2 后,我最终遇到了这个问题。

我只能通过设置一个新的虚拟环境,然后修改该虚拟环境中的 wstools 包以使用 BytesIO 而不是 StringIO 来解决此问题。

替换 StringIO 的每个必需实例。例如:

# WSDLTools.py
...
from IO import BytesIO
...
return self.loadFromStream(BytesIO(data))
Run Code Online (Sandbox Code Playgroud)

不理想,但它有效。比将所有内容迁移到 Python 3 更容易...