Using python zeep to consume web service. How to handle this response? - python

I am using a zeep client to consume a web service that is supposed to return me a file in base64binary format. I am using the following code.
try:
client = Client('https://host.com/xmlpserver/services/v2/CatalogService?wsdl')
response = client.service.downloadObject('/~fin_impl/INVITEM20.xdo','username','password')
print(response)
except zeepExceptions.Fault as fault:
print(fault.detail)
However, I am not sure what format is this response in and how to decode it.
b'PK\x03\x04\x14\x00\x08\x08\x08\x00o\xb8\x9cP\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00_report.xdo\x8dT\xcbN\xdc0\x14\xdd\xf3\x15V6\xacH\x02tQ\x01\x03*/i\xa4\x99\x01\r\x94v\x87\x8cs\x87\xb1\xf0K\xf65\xc9t\xd1o\xefu^\xa2\x14\xaaI\x94\x85\x8f\x8f}\xcf\xb99\xf6\xc9Y\xa3\x15{\x05\x1f\xa45l\xc2v\xf7\xf3r\x97\x81\x11\xb6\x92\xe69\x01\x11W{_w\xcfNwN<8\xeb\x91\xd1\x02\x13&\xd9\x1a\xd1\x1d\x15E;\xca\xad\xe7BA.\xac.l\xe3\x12\xe8\xb2\x8ex\xd4\x84j$\xd7\xf4\xe4\xf5!\xd1\x9f\x8b\x83\xb2\xdc/~\xcegwb\r\x9ag\x83\x86Iv\x90\x97\x19\xab8\xf2\xb9\xad#M2\xf4\x112\x16\x03\x9cKw\xcb=\xd7\x80D\x1dp\xe7m\x15\x05\xf8\x05\xe1\x93l%\xcd\x1e}\xdc\x08\xc9\xd5\x85\xd5\xda\x1a\xa2\x0c\x8b\x1en:\xd6\x1b\xe8\x9e\x87\x97ke\xeb\x04\x8a\x18\xd0\xeaK\xaa|a\rz\xabZ\x90\x93\xbe[\xf0\xdf\x03\xf8\xa1f\x8b\xddi\xeeq\t+\x0faM\x85\xb9\n\xc3L\xa2\xbe\x9b8\xdda\x8c\x9d\x8c\xa6X\xf4\xb4y\xf1\xfbz\xbax\x9c\xceog\xc5t\xf10\xbd\xbf\x9a\xe7M\xa5\xb3\xa2\'C\x10^:\xa4\x9e\xf4\x08Yu\xe0q\xc3L\xeb"\xacm\xdd\x0b\r\xd4>\xae"\xf4\x02?\xe6[\xa3\xa4\x81\xad\x98\x0e\xccL\x9a\x97\xa9Y#\xfdC\x9a\xca\xd6\xdb,\xe3\x11\xed2\x9a\x91\xdaY\xff\x98\xdb6\xea\xd2\x8a\xa8\xc1\xe06\x9b\'\xb7\xcb6\x80I\xd9V\x86y\xd8\x18\xb1\xf6\xd6\xd8\x18\xb6\xd5\xe5\xbc\xb4^\xe2f$\x1b\xeb5W\xffu\x11=O\x7fi\\rX~F\xef\xfe\x16T\xe7\x9b\xab\x06\xbf9\xf7\x89\xa41\xe4]N)}\xe49m\x9c\xb1\x80\x1b\x95d\x0e\x94\x99\x15m\xf1#\n\xfe\x9a\x94\xff\xa2\x12\\\x1dSa\xedp\x9c\x0c\xb2\x82\xe3a{\x04\xed\x14G\x08\xac\x82\x15\x8f\n\'Y\x1f\xbf\xc7\x83\x92\xde\xfd\xb2\xfc\xd2\x05\xf6-\x9b)\xfe\x94N\xe3\xbf\xd4.\xcco\xf1\x04\xe7MJ%n\x1c\xa9\x85F\x80\xca\x98\x8d\xe8"^\xa7\x86\xe2\x08\xf6\x1a\xde\xa1\x8a\x94\'\x9f#]\xadd\xe0O\n\xe6<\xa4\xf3\xda\xcb\x19\x8e"\x17(_\xc7\xd1\xab\x84\xfa\xa6\x8d\xf9\xdf\xc1(F\xcft\x8d\x15\xdd=v\xba\xf3\x07PK\x07\x08\xe8\xc9\xba\xb2-\x02\x00\x00\xfc\x04\x00\x00PK\x03\x04\x14\x00\x08\x08\x08\x00o\xb8\x9cP\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\x00\x00\x00INVITEM_20201004.xls.png\x03\x00PK\x07\x08\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00PK\x03\x04\x14\x00\x08\x08\x08\x00o\xb8\x9cP\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x17\x00\x00\x00INVITEM_20201004_en.xls\xed]\x07\\SI ........'
Please suggest, Thanks

Related

Python and socket - connet to specific path

I need to connect/send msg to http://localhost:8001/path/to/my/service, but I am not able to find how to do that. I know how to send if I only have localhost and 8001, but I need this specific path /path/to/my/service. There is where my service is running.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(<full-url-to-my-service>)
s.sendall(bytes('Message', 'utf-8'))
Update
My service is running on localhost:8001/api/v1/namespaces/my_namespace/services/my_service:http/proxy. How can I connect to it with python?
As #furas told in the comments
socket is primitive object and it doesn't have specialized method for this - and you have to on your own create message with correct data. You have to learn HTTP protocol and use it to send
This is a sample snippet to send a GET request in python using requests library
import requests
URL = 'http://localhost:8001/path/to/my/service'
response_text = requests.get(URL).text
print(response_text)
This assumes the Content-Type that GET URL produces is text. If it is json, then a minor change is required
import requests
URL = 'http://localhost:8001/path/to/my/service'
response_json = requests.get(URL).json()
print(response_json)
There are other ways to achieve the same using other good frameworks like urllib, and so on.
Here is the documentation of requests library for reference
sendall() requires bytes, so String must be encoded.
s.sendall("foobar".encode())

How to connect with an API that requires username and password

I am trying to connect to the api as explained in http://api.instatfootball.com/ , It is supposed to be something like the following get /[lang]/data/[action].[format]?login=[login]&pass=[pass]. I know the [lang], [action] and [format] I need to use and I also have a login and password but donĀ“t know how to access to the information inside the API.
If I write the following code:
import requests
r = requests.get('http://api.instatfootball.com/en/data/stat_params_players.json', auth=('login', 'pass'))
r.text
with the actual login and pass, I get the following output:
{"status":"error"}
This API requires authentication as parameters over an insecure connection, so be aware that this is highly lacking on the API part.
import requests
username = 'login'
password = 'password'
base_url = 'http://api.instatfootball.com/en/data/{endpoint}.json'
r = requests.get(base_url.format(endpoint='stat_params_players'), params={'login': username, 'pass': password})
data = r.json()
print(r.status_code)
print(r.text)
You will need to make a http-request using the URL. This will return the requested data in the response body. Depending on the [format] parameter, you will need to decode the data from xml / json to a native Python object.
As rdas already commented, you can use the request library for python (https://requests.readthedocs.io/en/master/). You will also find some code samples there. It will also do proper decoding of JSON data.
If you want to play around with the API a bit, you can use a tool like Postman for testing and debugging your requests. (https://www.postman.com/)

Python SUDS - Getting Exception 415 when calling a SOAP method

from suds.client import Client
url = r'http://*********?singleWsdl'
c = Client(url)
The requests work fine till here, but when I execute the below statement, I get the error message shown at the end. Please help.
c.service.Method_Name('parameter1', 'parameter2')
The Error message is :
Exception: (415, u'Cannot process the message because the content type
\'text/xml; charset=utf-8\' was not the expected type
\'multipart/related; type="application/xop+xml"\'.')
A Content-Type header of multipart/related; type="application/xop+xml" is the type used by MTOM, a message format used to efficiently send attachments to/from web services.
I'm not sure why the error claims to be expecting it, because the solution I found for my situation was the override the Content-Type header to 'application/soap+xml;charset=UTF-8'.
Example:
soap_client.set_options(headers = {'Content-Type': 'application/soap+xml;charset=UTF-8'})
If you are able, you could also trying checking for MTOM encoding in the web service's configuration and changing it.

WebService request with python suds by XML

i'm trying to comunicate with a webserver with python. I'm using the suds library. Actually i'm pretty new with this.
Usually, to comunicate with this WebServer a send a xml message and i get a response. So this is what i would like to do with python.
Here's the code i wrote:
from suds.client import Client
with open("PATH","r") as f:
file=f.read()
url='URL'
client = Client(url)
httpHeaders = {'Content-Type': 'text/xml', 'SOAPAction': 'ACTION'}
client.set_options(headers=httpHeaders)
Now i don't know how to make the request. I tried this:
print client.service.test(__inject={'msg': file})
But i got the error:
Exception: No services defined
The problem seems clear, but i don't know haw to procede. Any suggestion ?

sending data to xml api of webservice

Im trying to write a python script that basically interacts with a webservice that uses an xml api. The request method is POST.
Usually I would write a request of the form request(url, data, headers) - however, in the case of an xml api it would not work. Also something like data.encode('utf-8') or urllib.urlencode(data) would not work as the data is not a dict.
In this case, data is xml so how am i supposed to sent it over?
[EDIT]
When I send a string of XML I get a urllib2.HTTPError: HTTP Error 415: Unsupported Media Type Exception. Is there any other way I'm supposed to send the data?
Also, the API I am using the Google Contacts API. I'm trying to write a script that adds a contact to my gmail account.
You probably need to set proper Content-Type header, for XML it would probably be:
application/xml
So something like this should get you going:
request = urllib2.Request( 'xml_api.example.com' )
request.add_header('Content-Type', 'application/xml')
response = urllib2.urlopen(request, xml_data_string)
Hope that helps :)

Categories