使用Python+flask调用webService

tkpromise
  • 90

我通过学习Flask Web(就是封面是一条狗的那本书)构建了一个网站,在登录页面需要调用WebService提供的接口,我们这边的另一个开发人员给了我一个WebService的接口让我调用,但我不知道怎么调用,希望大家帮我看一下,下面的数据如何通过Python调用。

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /MemberService.asmx HTTP/1.1
Host: 114.55.172.*
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.*.com/MemberLogin"

<?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:Body>
    <MemberLogin xmlns="http://www.*.com/">
      <sCondition>string</sCondition>
      <sPassword>string</sPassword>
    </MemberLogin>
  </soap:Body>
</soap:Envelope>


HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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:Body>
    <MemberLoginResponse xmlns="http://www.*.com/">
      <MemberLoginResult>
        <MebID>int</MebID>
        <CreateTime>dateTime</CreateTime>
        <State>NotActive or Active or Vain</State>
        <StayPlaceID>int</StayPlaceID>
        <MebTypeName>string</MebTypeName>
        <SellerName>string</SellerName>
      </MemberLoginResult>
    </MemberLoginResponse>
  </soap:Body>
</soap:Envelope>
评论
阅读 2.6k
2 个回答
import requests

url = "你的域名/login"

payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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/\">\n  <soap:Body>\n    <MemberLogin xmlns=\"http://www.*.com/\">\n      <sCondition>string</sCondition>\n      <sPassword>string</sPassword>\n    </MemberLogin>\n  </soap:Body>\n</soap:Envelope>"
headers = {
    'cache-control': "no-cache",
    'postman-token': "2b4bb59b-2d29-17a9-3bfd-986d15597e40"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

这就是用 Python 来操作 XML 而已。

撰写回答

登录后参与交流、获取后续更新提醒

宣传栏