2014-04-10 195 views
0

我知道有很多類似的問題 - 這樣的解決方案,但沒有解決方案爲我工作。訪問網絡服務

我想訪問Web服務,但得到HTTP錯誤:401未經授權。

這裏是我的代碼:

from suds.transport.http import HttpAuthenticated 
from suds.client import Client 
import base64 
url = 'http://server.something.com/path/to/Service.svc' 

transport = HttpAuthenticated() 
client = Client(url,username='******', password='********') 

並得到如下錯誤:

Traceback (most recent call last): 
File "./getDetails.py", line 127, in <module> 
test() 
File "./getDetails.py", line 121, in test 
client = Client(url,username='****', password='******') 
File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-    py2.7.egg/suds/client.py", line 109, in __init__ 
self.wsdl = Definitions(url, options) 
File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7- py2.7.egg/suds/wsdl.py", line 172, in __init__ 
root = p.parse(url=url).root() 
File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/sax/parser.py", line 133, in parse 
fp = self.transport.open(Request(url)) 
File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/transport/https.py", line 69, in open 
return HttpTransport.open(self, request) 
File "/usr/local/lib/python2.7/dist-packages/suds-0.3.7-py2.7.egg/suds/transport/http.py", line 72, in open 
raise TransportError(str(e), e.code, e.fp) 
suds.transport.TransportError: HTTP Error 401: Unauthorized 

任何幫助將不勝感激。提前致謝。

+0

你好!我認爲你需要設置你的用戶名和密碼作爲標題。您的服務是否暴露任何驗證方法?或有任何認證對象? – israelord

+0

對不起,我沒有得到你。 – mrutyunjay

回答

0

不知道這個解決您的問題,但我建議你使用這個庫Requests

import requests 
response = requests.get('https://api.github.com/user', auth=('user', 'pass')) 

根據你的代碼,你指向一個根服務而不是一個方法。

+0

如何在此之後調用方法?在我的代碼中,我首先嚐試初始化客戶端。並使用我可以調用方法。 – mrutyunjay

+0

對不起,我想念你的代碼,想念你的服務是肥皂基吧? – BlaShadow

+0

是的。那就對了。 – mrutyunjay