python response does not match with soapUI - python

When I access the web service using soapUI I get the correctly formatted text.
But when I use the python code, I get a dictionary with all rows in a single allBusType key.
from pysimplesoap.client import SoapClient
url = 'http://180.92.171.93:8080/UPSRTCServices/UPSRTCService?wsdl'
namespace = 'http://service.upsrtc.trimax.com/'
client = SoapClient(wsdl=url, namespace=namespace, trace=True)
print client.GetBusTypes()
The above code returns the following:
{'return': {'allBusType': [{'busName': u'AC SLEEPER'}, {'busType': u'ACS'}, {'ischildconcession': u'N'}, {'isseatlayout': u'N'}, {'isseatnumber': u'N'}, {'busName': u'AC-JANRATH'}, {'busType': u'JNR'}, {'ischildconcession': u'N'}, {'isseatlayout': u'Y'}, {'isseatnumber': u'Y'},....
As per the following screen, soapUI is returning all the bus stops as separate tag. (And not all stops in a single tag as above)
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:GetBusTypesResponse xmlns:ns2="com.trimax.upsrtc.xml.jaxb.model" xmlns:ns3="http://service.upsrtc.trimax.com/">
<return>
<allBusType>
<busName>AC SLEEPER</busName>
<busType>ACS</busType>
<ischildconcession>N</ischildconcession>
<isseatlayout>N</isseatlayout>
<isseatnumber>N</isseatnumber>
</allBusType>
<allBusType>
<busName>AC-JANRATH</busName>
<busType>JNR</busType>
<ischildconcession>N</ischildconcession>
<isseatlayout>Y</isseatlayout>
<isseatnumber>Y</isseatnumber>
</allBusType>
I will like to know if this is python issue or the server issue.
For each entry there is opening and closing tag called "allBusType" in the soapUI response that is missing in python response.
Python output is returning a single row for all entries.

SoapClient returns a SimpleXmlElement as stated in the first line of the SoapClient docs:
A simple, minimal and functional HTTP SOAP webservice consumer, using httplib2 for the connection ad SimpleXmlElement for the XML request/response manipulation.
Therefore to view it as xml you need to call the as_xml method on the returned SimpleXmlElement:
as_xml(pretty=False): Return the XML representation of the document
The following should work:
from pysimplesoap.client import SoapClient
url = 'http://180.92.171.93:8080/UPSRTCServices/UPSRTCService?wsdl'
namespace = 'http://service.upsrtc.trimax.com/'
client = SoapClient(wsdl=url, namespace=namespace, trace=True)
results = client.GetBusTypes()
print results.as_xml()

Related

Http Post data from Python to net core app

I am trying to send a file from a Python script to my .net core webserver.
In Python I am doing this using the requests library, and my code looks like so.
filePath = "run-1.csv"
with open(filePath, "rb") as postFile:
file_dict = {filePath: postFile}
response = requests.post(server_path + "/batchUpload/create", files=file_dict, verify=validate_sql)
print (response.text)
This code executes fine, and I can see the request fine in my webserver code which looks like so:
[HttpPost]
[Microsoft.AspNetCore.Authorization.AllowAnonymous]
public string Create(IFormFile file) //Dictionary<string, IFormFile>
{
var ms = new MemoryStream();
file.CopyTo(ms);
var text = Encoding.ASCII.GetString(ms.ToArray());
Debug.Print(text);
return "s";
}
However, the file parameter always returns as null.
Also, I can see the file parameter fine when getting data posted from postMan
I suspect that this problem has to do with how .net core model binding works, but not sure...
Any suggestions here on how to get my file displaying on the server?
Solved my issue - the problem was that in Python I was assigning my file to my upload dictionary with the actual file name "./run1.csv" rather than a literal string "file"
Updating this fixed my issue.
file_dict = {"file": postFile}
This is what I believe #nalnpir mentioned above.
I figured this out by posting from postman and also from my python code to http://httpbin.org/post and comparing the respoinse
The example from the requests docs is mostly correct, except that the key has to match the parameter of the controller method signature.
url = 'https://www.url.com/api/post'
files = {'parameterName': open('filename.extension', 'rb')}
r = requests.post(url, files=files)
So in this case the controller action should be
[HttpPost]
public string Post(IFormFile parameterName)

Getting values from XML Url Python

I have an account in https://es.besoccer.com/ and they have an api for getting data in a xml.
I have this code in python for print the values of the xml I need:
from xml.dom import minidom
doc = minidom.parse("datos.xml")
partidos = doc.getElementsByTagName("matches")
for partido in partidos:
local = partido.getElementsByTagName("local")[0]
visitante = partido.getElementsByTagName("visitor")[0]
print("local:%s" % local.firstChild.data)
print("visitante:%s" % visitante.firstChild.data)
canales=partido.getElementsByTagName("channels")
for canal in canales:
nombre=canal.getElementsByTagName("name")[0]
print("canal:%s" % nombre.firstChild.data)
The problem is thatthe XML of this site is a url so I donĀ“t know how to read the xml directly form the url. Other problem is that the xml contains some tags that are a link, and python throughs a error with that tags that contains a url.
Read the API docs here: https://www.besoccer.com/api/documentacion
After you understand which API call you need to use, prepare the URL and the query arguments and use a library like requests in order to read the data.
Once you have the reply (assuming it is XML based) - you can use your code and parse it.

Python Zeep - List Object has no Attribute _xsd_name

I'm trying to call a SOAP endpoint using the Zeep library.
The endpoint has the following signature:
ExecuteStaticSQLQuery(queryType: ns0:StaticQueries, xmlParameters: {_value_1: ANY}, iRowsAffected: xsd:int) -> ExecuteStaticSQLQueryResult: xsd:int, iRowsAffected: xsd:int
I built a function that contains the following logic:
INSERT_AUTO_FORWARD_IMPORT = 'INSERT_AUTO_FORWARD_IMPORT_1'
query_parameter = self.client.get_type('ns0:QueryParameter')
db_type = self.client.get_type('ns0:DbType')
table_parameter = query_parameter(ParameterType=db_type('String'),
ParameterName='#Auto_Forward_Table_Name',
ParameterValue=auto_forward_table)
any_object = AnyObject(query_parameter, table_parameter)
xml_parameters = {
'_value_1': any_object
}
soap_envelope = self.service.ExecuteStaticSQLQuery(queryType=INSERT_AUTO_FORWARD_IMPORT,
xmlParameters=xml_parameters,
iRowsAffected=rows_affected)
When I call the function I get the following error:
'list' object has no attribute '_xsd_name'
I suspect the issue is coming from the any_object I build, because I can send a request to the endpoint without the xmlParameters argument and I get a response back.
Based on the Zeep Any Objects Docs, I would think I'm correctly constructing the AnyObject and implementing the call to the SOAP endpoint.
I'm not really sure where to go from here.
I'm not sure you are correctly creating the AnyObject.
According to the Zeep docs you link to, you should be calling get_element, not get_type, to construct the AnyObject.
In other words, try replacing the line
query_parameter = self.client.get_type('ns0:QueryParameter')
with
query_parameter = self.client.get_element('ns0:QueryParameter')

python django soaplib response with classmodel issue

I run a soap server in django.
Is it possible to create a soap method that returns a soaplib classmodel instance without <{method name}Response><{method name}Result> tags?
For example, here is a part of my soap server code:
# -*- coding: cp1254 -*-
from soaplib.core.service import rpc, DefinitionBase, soap
from soaplib.core.model.primitive import String, Integer, Boolean
from soaplib.core.model.clazz import Array, ClassModel
from soaplib.core import Application
from soaplib.core.server.wsgi import Application as WSGIApplication
from soaplib.core.model.binary import Attachment
class documentResponse(ClassModel):
__namespace__ = ""
msg = String
hash = String
class MyService(DefinitionBase):
__service_interface__ = "MyService"
__port_types__ = ["MyServicePortType"]
#soap(String, Attachment, String ,_returns=documentResponse,_faults=(MyServiceFaultMessage,) , _port_type="MyServicePortType" )
def sendDocument(self, fileName, binaryData, hash ):
binaryData.file_name = fileName
binaryData.save_to_file()
resp = documentResponse()
resp.msg = "Saved"
resp.hash = hash
return resp
and it responses like that:
<senv:Body>
<tns:sendDocumentResponse>
<tns:sendDocumentResult>
<hash>14a95636ddcf022fa2593c69af1a02f6</hash>
<msg>Saved</msg>
</tns:sendDocumentResult>
</tns:sendDocumentResponse>
</senv:Body>
But i need a response like this:
<senv:Body>
<ns3:documentResponse>
<hash>A694EFB083E81568A66B96FC90EEBACE</hash>
<msg>Saved</msg>
</ns3:documentResponse>
</senv:Body>
What kind of configurations should i make in order to get that second response i mentioned above ?
Thanks in advance.
I haven't used Python's SoapLib yet, but had the same problem while using .NET soap libs. Just for reference, in .NET this is done using the following decorator:
[SoapDocumentMethod(ParameterStyle=SoapParameterStyle.Bare)]
I've looked in the soaplib source, but it seems it doesn't have a similar decorator. The closest thing I've found is the _style property. As seen from the code https://github.com/soaplib/soaplib/blob/master/src/soaplib/core/service.py#L124 - when using
#soap(..., _style='document')
it doesn't append the %sResult tag, but I haven't tested this. Just try it and see if this works in the way you want it.
If it doesn't work, but you still want to get this kind of response, look at Spyne:
http://spyne.io/docs/2.10/reference/decorator.html
It is a fork from soaplib(I think) and has the _soap_body_style='bare' decorator, which I believe is what you want.

urllib2.urlopen not getting all content

I am a beginner in python to pull some data from reddit.com
More precisely, I am trying to send a request to http:www.reddit.com/r/nba/.json to get the JSON content of the page and then parse it for entries about a specific team or player.
To automate the data gathering, I am requesting the page like this:
import urllib2
FH = urllib2.urlopen("http://www.reddit.com/r/nba/.json")
rnba = FH.readlines()
rnba = str(rnba[0])
FH.close()
I am also pulling the content like this on a copy of the script, just to be sure:
FH = requests.get("http://www.reddit.com/r/nba/.json",timeout=10)
rnba_json = FH.json()
FH.close()
However, I am not getting the full data that is presented when I manually go to
http://www.reddit.com/r/nba/.json with either method, in particular when I call
print len(rnba_json['data']['children']) # prints 20-something child stories
but when I do the same loading the copy-pasted JSON string like this:
import json
import urllib2
fh = r"""{"kind": "Listing", "data": {"modhash": ..."""# long JSON string
r_nba = json.loads(fh) #loads the json string from the site into json object
print len(r_nba['data']['children']) #prints upwards of 100 stories
I get more story links. I know about the timeout parameter but providing it did not resolve anything.
What am I doing wrong or what can I do to get all the content presented when I pull the page in the browser?
To get the max allowed, you'd use the API like: http://www.reddit.com/r/nba/.json?limit=100

Categories