How to Connect with a SOAP Service Using Python
By hientd, at: 10:27 Ngày 03 tháng 7 năm 2024
How to Connect with a SOAP Service Using Python
Connecting with a SOAP (Simple Object Access Protocol) service can seem daunting at first, especially if you are more familiar with REST APIs. However, with the right tools and a step-by-step approach, you can easily integrate SOAP services into your Python applications. In this blog post, we'll walk through the process of connecting to a SOAP service using Python.
What is SOAP?
SOAP is a protocol used for exchanging structured information in the implementation of web services. It relies on XML-based messaging and is known for its robustness and extensibility. While RESTful services have gained popularity, SOAP is still widely used in enterprise environments due to its built-in security and transaction management features.
Prerequisites
Before we get started, make sure you have the following:
- Python Installed: Ensure that Python is installed on your machine. You can download it from python.org.
- Requests Library: Although not required for SOAP specifically, having the
requests
library can be helpful for handling HTTP requests. Install it using pip:pip install requests
- Zeep Library: Zeep is a powerful SOAP client for Python that simplifies the process of connecting to SOAP services. Install it using pip:
pip install zeep
Step-by-Step Guide
1. Understand the WSDL
The first step in connecting to a SOAP service is to understand its WSDL (Web Services Description Language) file. The WSDL file describes the service, including the available functions and their parameters. Most SOAP services provide a URL to access their WSDL file.
For this example, let's assume we have a SOAP service with a WSDL located at http://www.dneonline.com/calculator.asmx?wsdl
2. Import the Necessary Libraries
Start by importing the necessary libraries in your Python script:
import zeep
3. Create a Client
Next, create a client using the WSDL URL. This client will allow you to interact with the SOAP service.
In [2]: wsdl = 'http://www.dneonline.com/calculator.asmx?wsdl'
In [3]: client = zeep.Client(wsdl=wsdl)
In [4]: dir(zeep)
Out[4]:
['AnyObject',
'AsyncClient',
'CachingClient',
'Client',
'Plugin',
'Settings',
'Transport',
'__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'__version__',
'client',
'exceptions',
'helpers',
'loader',
'ns',
'plugins',
'proxy',
'settings',
'transports',
'utils',
'wsa',
'wsdl',
'xsd']
4. Explore the Service
You can explore the available operations and their details using Zeep. This is useful for understanding what functions you can call and what parameters are required.
for service in client.wsdl.services.values():
for port in service.ports.values():
operations = port.binding._operations.values()
for operation in operations:
print(f"Operation: {operation.name}")
print(f"Input: {operation.input}")
print(f"Output: {operation.output}")
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34580="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f345b0="" at="" object="">
Operation: Subtract
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34670="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f346a0="" at="" object="">
Operation: Multiply
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34700="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f34730="" at="" object="">
Operation: Divide
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34790="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f347c0="" at="" object="">
Operation: Add
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34940="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f34970="" at="" object="">
Operation: Subtract
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34a30="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f34a60="" at="" object="">
Operation: Multiply
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34ac0="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f34af0="" at="" object="">
Operation: Divide
Input: <zeep.wsdl.messages.soap.documentmessage 0x109f34b50="" at="" object="">
Output: <zeep.wsdl.messages.soap.documentmessage 0x109f34b80="" at="" object=""></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage></zeep.wsdl.messages.soap.documentmessage>
5. Call a Service Operation
To call an operation, use the client object and provide the necessary parameters.
For this example, let's assume the service has an operation called GetUser
that requires a userId
parameter.
result = client.service.Add(intA=5, intB=3)
print(response)
6. Handle the Response
The response from the service will be an object containing the data returned by the service. You can access and manipulate this data as needed.
7. Full Example
Here is the complete code for connecting to a SOAP service and calling an operation:
from zeep import Client
# WSDL URL
wsdl = 'http://www.dneonline.com/calculator.asmx?wsdl'
# Create a client
client = Client(wsdl=wsdl)
# Call a method
result = client.service.Add(intA=5, intB=3)
print(f"The result of addition is: {result}")
result = client.service.Subtract(intA=10, intB=5)
print(f"The result of subtraction is: {result}")
result = client.service.Multiply(intA=4, intB=7)
print(f"The result of multiplication is: {result}")
result = client.service.Divide(intA=20, intB=4)
print(f"The result of division is: {result}")
#########################
The result of addition is: 8
The result of subtraction is: 5
The result of multiplication is: 28
The result of division is: 5
Conclusion
Connecting to a SOAP service using Python is straightforward with the help of the Zeep library. By following the steps outlined in this post, you can easily integrate SOAP services into your Python applications. Whether you're retrieving user information, processing transactions, or interacting with complex enterprise systems, Python and Zeep make it simple to work with SOAP APIs.