How to call a Web Service from Python?

08-30-2019 11:04 AM
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: SW 10 street 

Any samples showing the URL part and the variables part.


0 Kudos
5 Replies
MVP Regular Contributor


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''
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 =, data=payload)

# URL, Response and Status
Occasional Contributor III


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''
values = {'myAddress' : '100 SW 10 street'}

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

0 Kudos
MVP Regular Contributor


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.

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
MVP Regular Contributor


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 

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