Python Soap client WSDL functions - python

I am trying to create a python soap client with zeep.
But i can't figure out how to use the functions which are defined in the WSDL.
Here is my code:
from requests import Session
from requests.auth import HTTPBasicAuth
import zeep
from zeep.transports import Transport
session = Session()
session.auth = HTTPBasicAuth('admin', 'ip411')
transport_with_basic_auth = Transport(session=session)
client =
zeep.Client(wsdl='http://10.8.20.27/pbx10_00.wsdl',transport=transport_with_basic_auth)
client.service.Initialize('soap','test',True,True,True,True,True)
You can look at the WSDL here: www.innovaphone.com/wsdl/pbx10_00.wsdl

Related

Soap request works fine in Soapui but not in python - ZEEP - SUDS

I have a request to a soap webservice which is working fine in SoapUi. I'd like to call this webservice in a python script.
I tried some modules : zeep, suds,... but I always have an SSL error.
Here are the infos :
https://37.71XXXXXXACONYX?wsdl
Basic authentification : username + password
Authentification type : preemptive
I have the xml available in SoapUi.
Does someone have any ideas?
Thanks a lot !!
I tried :
from requests.auth import HTTPBasicAuth # or HTTPDigestAuth, or OAuth1, etc.
from requests import Session
from zeep import Client
from zeep.transports import Transport
session = Session()
session.auth = HTTPBasicAuth(user, password)
client = Client('https://37.71XXXXXXACONYX?wsdl',
transport=Transport(session=session))

Python Zeep is throwing unkown fault for soap request

I am new to using the framework zeep. I am trying to send a SOAP request . But I get the below incorrect data. I need to get the response in xml or csv format.
<Element {http://schemas.xmlsoap.org/soap/envelope/}Envelope at 0x7fabdc9ea888>
With the wsdl, I am able to fetch the correct output using SoapUI tool.
from requests import Session
from zeep import Client
from zeep.transports import Transport
from requests.auth import AuthBase, HTTPBasicAuth
import datetime
wsdl = 'http://XX.XXX.XX.XX:ZZZZ/TL/IM?wsdl'
session = Session()
session.auth = SymantecAuth('user','password', "http://XX.XXX.XX.XXX")
session.verify = False
transport = Transport(session=session)
client = Client(wsdl=wsdl, transport=transport)
request_data = {"platforms": "test", "platid": {"ID": "QI4552"}}
results=client.create_message(client.service, 'RetrieveID', request_data)
print(results)
Since you are creating the message, print(results) is just showing the message object created.
Instead this should work to print the message on screen:
from lxml import etree
# Your code follows here
results=client.create_message(client.service, 'RetrieveID', request_data)
# this will print the message to be sent to Soap service.
print(etree.tostring(results, pretty_print=True))
If you want to see the response of the RetrieveID operation. then do this instead (Provided this method is bound on 1st available binding):
response = client.service.RetrieveID(**request_data)
print(response)
Let us know if it doesn't work.

How do i get Ip Address from Azure Function Http request with Python

How i get IP Address from this request HTTP using python? I found some posts using C#, like this:
Get remote IP address in Azure function
I have this code:
import logging
import json
import socket
import getpass
import os
from requests import get
import azure.functions as func
from azure.common.client_factory import get_client_from_json_dict,get_client_from_cli_profile
from azure.mgmt.sql import SqlManagementClient
from azure.mgmt.resource import SubscriptionClient
from azure.common.credentials import ServicePrincipalCredentials
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
Myip = get('https://api.ipify.org').text
username = getpass.getuser()
return func.HttpResponse(
f"List of IPs: {str(Myip)}",
status_code=200
)
The image shows the result of the local request.
https://i.stack.imgur.com/AkZKm.png
The result of your request should be saved to a response variable.
Before you read the body of your response, you can use this bit of code to get the IP address. Say your response variable is called rsp
print rsp.raw._fp.fp._sock.getpeername()
There is a similar question here = How do I get the IP address from a http request using the requests library?
More info here, Python: get remote IP from HTTP request using the requests module
Update
Python3.8 with requests 2.22.0
resp = requests.get('https://www.google.com', stream=True)
resp.raw._connection.sock.getsockname()
You can get the source IP of the incoming request from the x-forwarded-for header in the req object. I use the following code to log it:
if "x-forwarded-for" in req.headers:
source_ip = req.headers["x-forwarded-for"].split(':')[0]
logging.info("Incoming request from IP: " + source_ip)

SOAP API Get Cookie

I'm using a SOAP API to get an authentication key with a cookie that is suppose to be returned.
from zeep import Client
client = Client("AuthenticationService.xml")
result = client.service.ValidateUser(username, password, "")
result
However with the result, I am getting a Boolean of True but no Cookie that contains the authentication key.
From the below picture, you can see that the same request using SoapUI returns a cookie. I'm wondering how I can do this in Python.
To be able to handle cookie, we must use requests.Session for the transport.
So a simple use case would look like this for you:
from zeep import Client
from requests import Session
from zeep.transports import Transport
session = Session()
# disable TLS verification
session.verify = False
transport = Transport(session=session)
client = Client("AuthenticationService.xml", transport=transport)
result = client.service.ValidateUser(username, password, "")
# then check cookie
client.transport.session.cookies
hope this helps.

Python "requests" library: HTTP basic authentication for each request

In a Python script I use the "Requests" library with HTTP basic authentication and a custom CA certificate to trust like this:
import requests
response = requests.get(base_url, auth=(username, password), verify=ssl_ca_file)
All requests I need to make have to use these parameters. Is there a "Python" way to set these as default for all requests?
Use Session(). Documentation states:
The Session object allows you to persist certain parameters across
requests.
import requests
s = requests.Session()
s.auth = (username, password)
s.verify = ssl_ca_file
s.get(base_url)

Categories

Resources