Python + Soap + XMLHTTPRequest - PullRequest
       9

Python + Soap + XMLHTTPRequest

0 голосов
/ 28 февраля 2011

Здравствуйте, я пытаюсь сделать SOAP-запрос, вот мой код:

# 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 библиотеки на их форумах. Библиотечный сервер мне за то, что мне нужно было сделать.

1 Ответ

3 голосов
/ 28 февраля 2011

HTTPSConnection принимает имя хоста, а не URL, поэтому оно должно быть

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

, и я считаю, что заголовок хоста добавлен для вас, так что вы можете пропустить это, но если это не так, тотакже должно быть именем хоста, а не URL.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...