要使用Python的requests庫進行SOAP Web服務調用,你需要構造一個XML請求并將其發送到Web服務的URL。以下是一個簡單的示例:
import requests
# 定義SOAP請求的XML內容
soap_request = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.example.com/">
<soapenv:Header/>
<soapenv:Body>
<web:YourOperation>
<!-- 在這里添加你的操作參數 -->
</web:YourOperation>
</soapenv:Body>
</soapenv:Envelope>
"""
# 設置請求頭,指定Content-Type為text/xml
headers = {
'Content-Type': 'text/xml',
'SOAPAction': 'http://www.example.com/YourOperation' # 替換為你的操作名稱
}
# 發送POST請求到Web服務的URL
url = 'http://www.example.com/your-soap-service-endpoint' # 替換為你的Web服務URL
response = requests.post(url, data=soap_request, headers=headers)
# 打印響應內容
print(response.content)
在這個示例中,我們首先定義了一個名為soap_request
的字符串,其中包含了SOAP請求的XML內容。然后,我們設置了請求頭,包括Content-Type
和SOAPAction
。最后,我們使用requests.post
方法發送了POST請求,并將響應內容打印出來。
請注意,你需要根據實際情況修改XML內容、SOAPAction和URL。