通过python请求或urllib发送Soap 1.1请求

时间:2018-12-05 03:58:04

标签: python soap python-requests urllib2

我了解正在阅读此内容的人的第一反应是说“啊,这是重复的请求”,但请相信我不是。我已经尝试了堆栈溢出中列出的所有示例来实现我想要的功能,但仍然没有做到。

在通过Zeep和Suds无法完成此操作后,我想通过urllib2或具有以下设置的请求将SoapUI中看到的以下详细信息发送给给定的WSDL

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:slab="http://m2.slab">
   <soapenv:Header/>
   <soapenv:Body>
      <slab:PLChecker>
         <!--Optional:-->
         <slab:request>
            <!--Optional:-->
            <slab:ServiceProviderReferenceNumber>?</slab:ServiceProviderReferenceNumber>
            <!--Optional:-->
            <slab:ServiceNumber>0235625452</slab:ServiceNumber>
         </slab:request>
      </slab:PLChecker>
   </soapenv:Body>
</soapenv:Envelope>

This is the SoapUI settings I've used

到目前为止,我已经尝试过httpbasic身份验证,但是似乎对如何创建正确的肥皂信封一无所知:

  • 编码
  • 用户名
  • 密码
  • 身份验证类型:不进行身份验证
  • WSS密码类型:PasswordText

对此,我们将提供任何帮助。 这是我到目前为止一直在使用的代码。

import requests
from requests.auth import HTTPBasicAuth
import requests
import base64
import urllib
import ssl
ssl.match_hostname = lambda cert, hostname: True

import logging

# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True


url="https://commontest/PService.svc?singleWsdl"
# headers = {'content-type': 'application/soap+xml'}
request_headers = {'content-type': 'text/xml; charset=utf-8'}

body = """<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:slab="http://m2.com.au/slab">
   <soapenv:Header/>
   <soapenv:Body>
      <slab:PLChecker>
         <!--Optional:-->
         <slab:request>
            <!--Optional:-->
            <slab:ServiceProviderReferenceNumber>?</slab:ServiceProviderReferenceNumber>
            <!--Optional:-->
            <slab:ServiceNumber>?</slab:ServiceNumber>
         </slab:request>
      </slab:PLChecker>
   </soapenv:Body>
</soapenv:Envelope>"""

response = requests.post(url,data=body, verify = False, auth = ('user', 'pass'), headers = request_headers)
print response

0 个答案:

没有答案