Python 调用WebService接口出错-suds.transport.TransportError: HTTP Error 401: Unauthori

使用Python调用WebService接口出现“suds.transport.TransportError: HTTP Error 401: Unauthorized”错误。

问题原因:在调用接口时没有进行身份验证,需要增加身份验证代码。

无身份验证(适用于WebService无身份验证的情况)
from suds.client import Client
test = Client('http://xxxx.xxx.xx.x:8080/WebService/ws/DeviceManageService?wsdl')
print test


需要身份验证(适用于WebService有身份验证的情况)
from suds.client import Client
from suds.transport.http import HttpAuthenticated

t = HttpAuthenticated(username='admin', password='admin')
test = Client('http://xxxx.xxx.xx.x:8080/WebService/ws/DeviceManageService?wsdl', transport=t)
print test

#调用SebService接口fun
test.service.fun()


如果仍然出错,可尝试下面的方案:
from suds.client import Client
from suds.transport.http import HttpAuthenticated
import urllib2

t = HttpAuthenticated(username='me', password='password')
t.handler = urllib2.HTTPBasicAuthHandler(t.pm)
t.urlopener = urllib2.build_opener(t.handler)
c = client.Client(url='http://xxx.xxx.xxx.xxx/path/to?WSDL',transport=t)


你可能感兴趣的:(webservice)