鍍金池/ 問答/Python  C#  HTML/ 使用Python+flask調(diào)用webService

使用Python+flask調(diào)用webService

我通過學(xué)習(xí)Flask Web(就是封面是一條狗的那本書)構(gòu)建了一個網(wǎng)站,在登錄頁面需要調(diào)用WebService提供的接口,我們這邊的另一個開發(fā)人員給了我一個WebService的接口讓我調(diào)用,但我不知道怎么調(diào)用,希望大家?guī)臀铱匆幌?,下面的?shù)據(jù)如何通過Python調(diào)用。

以下是 SOAP 1.2 請求和響應(yīng)示例。所顯示的占位符需替換為實際值。

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>
回答
編輯回答
若相惜

這就是用 Python 來操作 XML 而已。

2018年3月30日 16:52
編輯回答
久舊酒
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)
2017年8月20日 19:49