Dùng python call webservice XML SOAP

Sau một hồi vật vã tìm kiếm thì mới tìm thấy cách sử dụng python để gọi một 1 webservice XML sử dụng SOAP.
Webservice XML được tạo ra bởi .NET và sử dụng .NET để gọi thì đơn giản rồi. Bây giờ mình muốn sử dụng Python thì cũng khá phức tạp, tìm mãi mới thấy cách dùng:

Code:

import requests
url="http://xxx.xxx.xxx:8668/xxx/WHOLESALESTATISTIC.asmx?WSDL"
#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?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><WholesaleStatistic xmlns="http://tempuri.org/">  <Account>xxx</Account> <Password>pass@abc</Password> <sDate>2019-02-17</sDate></WholesaleStatistic>  </soap:Body></soap:Envelope>"""

response = requests.post(url,data=body,headers=headers)
print(response.content)

Kết quả sẽ trả về định dạng XML, từ đây có thể lấy dữ liệu ra bằng cách pase XML hoặc google…

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><WholesaleStatisticResponse xmlns="http://tempuri.org/"><WholesaleStatisticResult><newsubs>0</newsubs><deactivated>0</deactivated><portfolio>20</portfolio></WholesaleStatisticResult></WholesaleStatisticResponse></soap:Body></soap:Envelope>'

Một số link tham khảo thêm:

https://medium.com/@adriennedomingus/using-zeep-to-make-soap-requests-in-python-c575ea0ee954

https://www.geeksforgeeks.org/get-post-requests-using-python/

https://viblo.asia/p/xay-dung-mot-restful-api-don-gian-voi-python-va-flask-bJzKmMvDK9N

http://hoctuduylaptrinh.com/category/lap-trinh/lap-trinh-python/