How to call a Web Service from Python?

5535
5
08-30-2019 11:04 AM
JoseSanchez
Occasional Contributor III

Hello everyone,

I am looking for a sample source code in Python that shows how to call a web service from Python.

I need to make a call to a web service with a structure similar to this one:

https://wwww.example.com/GISAddress/Check.asmx/VerifyAddress?param1=100 SW 10 street 

Any samples showing the URL part and the variables part.

Thanks

0 Kudos
5 Replies
LanceCole
MVP Regular Contributor

Jose‌,

The easiest way to GET or POST a response from a web service from Python is using requests.  You do not note what version of python you are using so you may need to install requests using the command pip install requests.  You also do not indicate if your web service requires authentication.  The examples below do not use authentication but this can be included.  Depending upon the encoding your web service uses, there may be issues with how the <white spaces> are handled in your address string.  Some use "+" while others use "%20" or "%".

import requests

url = r'https://wwww.example.com/GISAddress/Check.asmx/VerifyAddress'
payload = {'param1':'100 SW 10 street'} # add paramaters to the dictionary as needed

# GET Only
r = requests.get(url)

# GET with parameters
r = requests.get(url, params=payload)

# POST with parameters
r = requests.post(url, data=payload)

# URL, Response and Status
r.url
r.text
r.status_code‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JoseSanchez
Occasional Contributor III

Hello,

I am currently using Python 2.7.10 and the web service does not require authentication. 

I am able to call the web service in Python using the script below and I get a return in XML format.

Questions: how do I parse the answer to get only the part I need a "Y' or  a "N"

This is what  I get on line:  t = ET.fromstring(the_page)

<string xmlns="http://www.example.comv/GISAddress">Y</string>

Source code

 import arcpy, urllib, urllib2,

url = r'https://wwww.example.com/GISAddress/Check.asmx/VerifyAddress'
values = {'myAddress' : '100 SW 10 street'}

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
t = ET.fromstring(the_page)

0 Kudos
LanceCole
MVP Regular Contributor

Jose,

Add one more line - t.text this should return just the 'Y' valve for your code example.  I assume you also have xml.etree.ElementTree as ET in your import statement and that there will only ever be a single line response from the web service.

JoseSanchez
Occasional Contributor III

Thank you Lance, t.text   works.

Could you please recommend any good documentation that explains how to call web services using   requests or  urllib, urllib2, and how to parse the results.

Thank you

0 Kudos
LanceCole
MVP Regular Contributor

Jose‌,

Take a look at the following references:

19.7. xml.etree.ElementTree — The ElementTree XML API — Python 2.7.16 documentation 

20.5. urllib — Open arbitrary resources by URL — Python 2.7.16 documentation 

20.6. urllib2 — extensible library for opening URLs — Python 2.7.16 documentation 

I would also recommend taking a look at using the Requests module noted in my original post.  It greatly simplifies HTML requests which can be cumbersome using urllib and urllib2.  

requests · PyPI 

https://2.python-requests.org/en/master/ 

https://2.python-requests.org/en/master/user/quickstart/#make-a-request 

https://realpython.com/python-requests/ 

I also recommend that you Google "GET and POST requests using Python", there is a lot of information out there.

If you are going to be using Python on a regular basis I highly recommend David Breazley's - Python Essential Reference.  I have the 4th addition and understand there is a new edition coming out in 2020.  This is a reference book and not a how to book but I have never used a programing book as much as I have this one.

0 Kudos