im trying to build an client for an webservice in python with suds. i used the tutorial
on this site: http://www.jansipke.nl/python-soap-client-with-suds. Its working with my own written Webservice and WSDL, but not with the wsdl file i got. The wsdl file is working in soapUI, i can send requests and get an answer. So the problem is, i think, how suds is parsing the wsdl file. I get following error:
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
Any ideas how to fix that? If you need more information please ask. Thank you!
The error you have given us seems to imply that the URL you are using to access the WSDL is not correct. could you show us a bit more of your code? for example the client instatiation and the url to the WSDL. this might allow others to actually help you.
Olly
# SUDS is primarily built for Python 2.6/7 (Lightweight SOAP client)
# SUDS does not work properly with other version, absolutely no support for 3.x
# Test your code with Python 2.7.12 (I am using)
from suds.client import Client
from suds.sax.text import Raw
# Use your tested URL same format with '?wsdl', Check once in SOAP-UI, below is dummy
# Make sure to use same Method name in below function 'client.service.MethodName'
url = 'http://localhost:8080/your/path/MethodName?wsdl'
#Use your Request XML, below is dummy, format xml=Raw('xml_text')
xml = Raw('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:diag=" </soapenv:Body></soapenv:Envelope>')
def GetResCode(url, xml):
client = Client(url)
xml_response = (client.service.MethodName(__inject={'msg':xml}))
return xml_response
print(GetResCode(url,xml))
Related
I set up a nextcloud instance and I would like to download files from there using a python script. My nextcloud instance enforces 2-factor authentication for all users and I want it to remain that way.
My dream scenario would be to use the requests library, so following the docs here https://docs.nextcloud.com/server/15/developer_manual/client_apis/WebDAV/basic.html , I tried to do something like this:
from requests.auth import HTTPBasicAuth
r = requests.request(
method='get',
url='https://mycloudinstance/index.php/apps/files/?dir=/Test&fileid=431',
auth=('username', 'pass')
)
print(r.status_code)
print(r.text)
That gives me an 401 error saying {"message":"Current user is not logged in"}.
When I change the above URL to https://remote.php/dav/myinstance/index.php/apps/files/?dir=/Test&fileid=431 I get
ConnectionError(': Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
As an alternative I was trying to use trying to use this library https://github.com/owncloud/pyocclient just to see if I can create a testfolder with it (it is from owncloud but should work with nextcloud too):
import owncloud
oc = owncloud.Client('https://mycloudinstance')
oc.login('username', 'pass')
oc.mkdir('cooldir')
This throws me an owncloud.owncloud.HTTPResponseError: HTTP error: 401 error. I think that might either be because I just use it incorrectly or because of 2-factor auth.
I am not sure how to use the webdav protocol combined with the python requests library and also I am not sure how to get two-factor authorization to work with it. Has anyone ever done this?
Help is very much appreciated, thanks so much in advance.
You can bypass the 2-factor authentication by generating a secure password for a single application.
In next cloud, go to: Settings -> Personal -> Security -> Create New App Password
The password will be shown to you once (and only once), use it in place of your normal password in your script.
I want to consume data from WSDL url, I don't know how would I do in python.
Can anyone help me with the example
here is the WSDL link for reference :
http://43.242.214.173/cwplservice/cwplonline.svc?wsdl
Here is code snippet which I have tried.
from suds.client import Client
from suds.xsd.doctor import Import, ImportDoctor
url = 'http://43.242.214.173/cwplservice/cwplonline.svc?wsdl'
imp = Import('http://212.235.42.50/WebService/service.php?class=masterPricer', location='https://www.w3.org/2001/XMLSchema.xsd')
imp.filter.add('http://www.w3.org/2001/XMLSchema')
client = Client(url, doctor=ImportDoctor(imp))
print client
when I run the code I am getting following error as
suds.TypeNotFound: Type not found: '(schema, http://www.w3.org/2001/XMLSchema, )'
Did you looked into PySimpleSOAP? I've used it, it is pretty good for making SOAP calls and parsing small size XML responses. For very large XML message responses it has crashed on me, ended up writing my own XML handler using xml.etree.ElemenTree.
https://pypi.python.org/pypi/PySimpleSOAP/1.02b
I've created some web services using pysimplesoap like on this documentation:
https://code.google.com/p/pysimplesoap/wiki/SoapServer
When I tested it, I called it like this:
from SOAPpy import SOAPProxy
from SOAPpy import Types
namespace = "http://localhost:8008"
url = "http://localhost:8008"
proxy = SOAPProxy(url, namespace)
response = proxy.dummy(times=5, name="test")
print response
And it worked for all of my web services, but when I try to call it by using an library which is needed to specify the WSDL, it returns "Could not connect to host".
To solve my problem, I used the object ".wsdl()" to generate the correct WSDL and saved it into a file, the WSDL generated by default wasn't correct, was missing variable types and the correct server address...
The server name localhost is only meaningful on your computer. Once outside, other computers won't be able to see it.
1) find out your external IP, with http://www.whatismyip.com/ or another service. Note that IPs change over time.
2) plug the IP in to http://www.soapclient.com/soaptest.html
If your local service is answering IP requests as well as from localhost, you're done!
I am trying to access the mongolab REST api through python. Is the correct way to do this via pythons urllib2? I have tried the following:
import urllib2
p = urllib2.urlopen("https://api.mongolab.com/api/1/databases/mydb/collections/mycollection?apiKey=XXXXXXXXXXXXXXXX")
But this gives me an error:
urllib2.URLError: <urlopen error unknown url type: https>
What is the correct way of doing this? After connecting, how do I go on to POST a document to my collection? If someone could post up a code example, I would be very grateful. Thanks all for the help!
EDIT:
I've recompiled python with ssl support. How do I POST insert a document to a collection using mongolab REST API? Here is the code I have atm:
import urllib
import urllib2
url = "https://api.mongolab.com/api/1/databases/mydb/collections/mycollection?apiKey=XXXXXXXXXXXXXXXX"
data = {"x" : "1"}
request = urllib2.Request(url, data)
p = urllib2.urlopen(request)
Now, when I run this, I get the error
urllib2.HTTPError: HTTP Error 415: Unsupported Media Type
How do I insert documents using HTTP POST? Thanks!
That error is raised if you version of python does not include ssl support. What version are you using? Did you compile it yourself?
That said, when you get a version including ssl, using requests is a lot easier than urllib2, especially when POSTing data.
I am using the following python code to display all methods offered by a webservice generated from a wsdl version 2.0 file.
The url is the following:
http://localhost:8080/axis2/services/UserService?wsdl2
Using the above url, the browser displays the wsdl file but when using this url in a python application below, it returns only the following info and nothing related to the webservice methods in question.
Python code
from suds.wsse import *
from suds.client import Client
myclient = Client("http://localhost:8080/axis2/services/UserService?wsdl2")
print myclient
output
Suds ( https://fedorahosted.org/suds/ ) version: 0.3.9 GA build: R659-20100219
it should be returing the methods available in the webservice as in the example https://fedorahosted.org/suds/wiki/Documentation
any idea?
Try removing the /tmp/suds directory. Also try passing cache=None in the Client constructor:
myclient = Client("http://localhost:8080/axis2/services/UserService?wsdl2", cache=None)
It seems that still suds doesn't support WSDL 2.
See https://fedorahosted.org/suds/ticket/479