Python+;肥皂+;XMLHTTPRequest

Python+;肥皂+;XMLHTTPRequest,python,soap,Python,Soap,您好,我正在尝试执行SOAP请求。以下是我的代码: # Create an OpenerDirector with support for Basic HTTP Authentication... auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password(realm='webservices.autotask.net', uri='https://webser

您好,我正在尝试执行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)
#创建一个支持基本HTTP身份验证的OpenerDirectory。。。
auth_handler=urllib2.HTTPBasicAuthHandler()
验证处理程序。添加密码(realm='webservices.autotask.net',
乌里https://webservices.autotask.net/atservices/1.5/atws.asmx',
用户=用户名,
passwd=密码)
opener=urlib2.build\u opener(auth\u处理程序)
#…并全局安装,以便与urlopen一起使用。
urllib2.install_opener(opener)
page=urlib2.urlopen('https://webservices.autotask.net/atservices/1.5/atws.asmx')
打印(第页,阅读(100))
SM_模板=”“
一串
一串
"""
SoapMessage=SM_模板%()
打印SoapMessage
#构造并发送标题
webservice=httplib.HTTPSConnection(“http://webservices.autotask.net", 443)
putrequest(“POST”,“/ATWS/v1_5/ATWS.asmx”)
putheader(“主机”http://webservices.autotask.net")
putheader(“用户代理”、“Python帖子”)
putheader(“内容类型”,“text/xml;字符集=\“UTF-8\”)
putheader(“内容长度”,“%d”%len(SoapMessage))
putheader(“SOAPAction”,“\”\“”)
打印(“标题”)
webservice.endheaders()
发送(SoapMessage)
#得到回应
statuscode,statusmessage,header=webservice.getreply()
打印(“响应:”、状态代码、状态消息)
打印(“标题:”,标题)
res=webservice.getfile().read()
打印(res)
我得到的错误是(顺便说一句,第69行是webservice.endheaders()):

回溯(最近一次呼叫最后一次):
文件“”,第1行,在
l()
文件“C:\Users\george\Documents\Autotask\Contract\AT Contract.py”,第69行,在l中
webservice.endheaders()
文件“C:\Python27\lib\httplib.py”,第937行,在endheaders中
自发送输出(消息体)
文件“C:\Python27\lib\httplib.py”,第797行,在发送输出中
self.send(msg)
文件“C:\Python27\lib\httplib.py”,第759行,在send中
self.connect()
文件“C:\Python27\lib\httplib.py”,第1140行,在connect中
self.timeout,self.source\u地址)
文件“C:\Python27\lib\socket.py”,第553行,位于create\u connection中
对于getaddrinfo(主机、端口、0、SOCK_流)中的res:
gaierror:[Errno 11004]getaddrinfo失败
有什么想法吗


编辑:我最终在他们的论坛上找到了一个.Net库。库服务器为我提供了我需要做的事情。

HTTPSConnection使用主机名,而不是URL,所以它应该是

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

我相信主机头是为您添加的,所以您可以跳过它,但如果不是,那么它也应该是主机名,而不是URL。

HTTPSConnection使用主机名,而不是URL,所以它应该是

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

我相信主机头是为您添加的,所以您可以跳过它,但如果不是,那也应该是主机名,而不是URL。

太好了,谢谢!这就解决了这个问题,但现在我得到了:404有什么想法吗?看起来路径不对,您有webservice.putrequest(“POST”,“/ATWS/v1_5/ATWS.asmx”),但前面的路径是/atservices/1.5/ATWS.asmxAwsome感谢您的帮助!我现在正在申请401,但我知道这是因为我没有正确的权限设置,而这要到明天才能实现。再次感谢!!!太好了,谢谢!这就解决了这个问题,但现在我得到了:404有什么想法吗?看起来路径不对,您有webservice.putrequest(“POST”,“/ATWS/v1_5/ATWS.asmx”),但前面的路径是/atservices/1.5/ATWS.asmxAwsome感谢您的帮助!我现在正在申请401,但我知道这是因为我没有正确的权限设置,而这要到明天才能实现。再次感谢!!!