2011-02-27 140 views
0

你好,我想在這裏做一個SOAP請求是我的代碼:的Python +肥皂+ XMLHTTPRequest的

# Create an OpenerDirector with support for Basic HTTP Authentication... 
auth_handler = urllib2.HTTPBasicAuthHandler() 
auth_handler.add_password(realm='webservices.autotask.net', 
          uri='https://webservices.autotask.net/atservices/1.5/atws.asmx', 
          user=username, 
          passwd=password) 
opener = urllib2.build_opener(auth_handler) 
# ...and install it globally so it can be used with urlopen. 
urllib2.install_opener(opener) 
page = urllib2.urlopen('https://webservices.autotask.net/atservices/1.5/atws.asmx') 
print (page.read(100)) 


SM_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header> 
    <AutotaskIntegrations xmlns="https://webservices.autotask.net/ATWS/v1_5/"> 
     <PartnerID>string</PartnerID> 
    </AutotaskIntegrations> 
    </soap:Header> 
    <soap:Body> 
    <getThresholdAndUsageInfo xmlns="https://webservices.autotask.net/ATWS/v1_5/"> 
     <sXML>string</sXML> 
    </getThresholdAndUsageInfo> 
    </soap:Body> 
</soap:Envelope> 

""" 
SoapMessage = SM_TEMPLATE%() 

print SoapMessage 


# construct and send the header 
webservice = httplib.HTTPSConnection("http://webservices.autotask.net", 443) 
webservice.putrequest("POST", "/ATWS/v1_5/atws.asmx") 
webservice.putheader("Host", "http://webservices.autotask.net") 
webservice.putheader("User-Agent", "Python post") 
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"") 
webservice.putheader("Content-length", "%d" % len(SoapMessage)) 
webservice.putheader("SOAPAction", "\"\"") 
print("HEADERS") 
webservice.endheaders() 
webservice.send(SoapMessage) 

# get the response 

statuscode, statusmessage, header = webservice.getreply() 
print ("Response: ", statuscode, statusmessage) 
print ("headers: ", header) 
res = webservice.getfile().read() 
print (res) 

我得到的錯誤(BTW行69 webservice.endheaders()):

Traceback (most recent call last): 
File "<pyshell#89>", line 1, in <module> 
l() 
File "C:\Users\george\Documents\Autotask\Contract\AT Contract.py", line 69, in l 
webservice.endheaders() 
File "C:\Python27\lib\httplib.py", line 937, in endheaders 
self._send_output(message_body) 
File "C:\Python27\lib\httplib.py", line 797, in _send_output 
self.send(msg) 
File "C:\Python27\lib\httplib.py", line 759, in send 
self.connect() 
File "C:\Python27\lib\httplib.py", line 1140, in connect 
self.timeout, self.source_address) 
File "C:\Python27\lib\socket.py", line 553, in create_connection 
for res in getaddrinfo(host, port, 0, SOCK_STREAM): 
gaierror: [Errno 11004] getaddrinfo failed 

任何想法?

編輯:我最終在他們的論壇上找到了一個.Net庫。圖書館服務我爲我需要做的事情。

回答

3

HTTPSConnection需要一個主機名,而不是一個URL,所以應該

webservice = httplib.HTTPSConnection("webservices.autotask.net", 443) 

而且我相信,主機頭增加了你,所以你可以跳過,但如果它不是,應該也是主機名稱,而不是URL。

+0

非常感謝你!那固定的,但現在我得到了:一個404任何想法? – George 2011-02-27 22:05:42

+0

看起來像錯誤的路徑,你有webservice.putrequest(「POST」,「/ATWS/v1_5/atws.asmx」),但更早的路徑是/atservices/1.5/atws.asmx – superfell 2011-02-27 22:31:02

+0

Awsome感謝您的幫助!我現在得到了401,但我知道這是因爲我沒有正確的權限設置,直到明天才會發生。再次感謝! – George 2011-02-27 22:48:07