0% found this document useful (0 votes)
12 views

(L8)Programming with Python (Intermediate Level)

This lesson covers the use of web services in Python, focusing on data exchange formats like XML and JSON. It explains XML's structure, terminology, and basic parsing, as well as JSON's key-value mapping and parsing in Python. Additionally, it discusses APIs, their security considerations, and the choice between SOAP and REST for building APIs.

Uploaded by

hobohin914
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

(L8)Programming with Python (Intermediate Level)

This lesson covers the use of web services in Python, focusing on data exchange formats like XML and JSON. It explains XML's structure, terminology, and basic parsing, as well as JSON's key-value mapping and parsing in Python. Additionally, it discusses APIs, their security considerations, and the choice between SOAP and REST for building APIs.

Uploaded by

hobohin914
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Programming with Python

(Intermediate Level)
Lesson-8 Using Web Services

Coding ညေကျာင်း by Code Champs Coding School


Data on the Web
● With the HTTP Request/Response well understood and well supported, there
was a natural move toward exchanging data between programs using these
protocols
● We needed to come up with an agreed way to represent data going between
applications and across networks
● There are two commonly used formats: XML and JSON
XML - eXtensible Markup Language
Marking up data to send across the network…

Primary purpose is to help information systems share structured data.

It started as a simplified subset of the Standard Generalized Markup Language


(SGML), and is designed to be relatively human-legible
XML Terminology
● Tags indicate the beginning and ending of elements
● Attributes - Keyword/value pairs on the opening tag of XML
● Serialize / Deserialize - Convert data in one program into a common format
that can be stored and/or transmitted between systems in a programming
language-independent manner
XML Basics
● Start Tag
● End Tag
● Text Content <person>

● Attribute <name>Chuck</name>
● Self Closing Tag
<phone type="intl">

+1 734 303 4456

</phone>

<email hide="yes" />

</person>
import xml.etree.ElementTree as ET
data = ''' xmlParsing.py
<person>
<name>Chuck</name>
<phone type="intl">+1 734 303 4456</phone>
<email hide="yes"/>
</person>
'''
tree = ET.fromstring(data)
print('Name:',tree.find('name').text)
print('Attr:',tree.find('email').get('hide'))
import xml.etree.ElementTree as ET

input = '''
xmlParsing2.py
<stuff>

<users>

<user x="2"> stuff = ET.fromstring(input)


<id>001</id>
lst = stuff.findall('users/user')
<name>Chuck</name>
print('User count:', len(lst))
</user>

<user x="7"> for item in lst:

<id>009</id> print('Name', item.find('name').text)


<name>Brent</name> print('Id', item.find('id').text)
</user>
print('Attribute', item.get("x"))
</users>

</stuff>

'''
JavaScript Object Notation(JSON)
The full-form of JSON is JavaScript Object Notation.
It means that a script (executable) file which is made of text in a programming
language, is used to store and transfer the data.
Python supports JSON through a built-in package called json.
To use this feature, we import the json package in Python script.
The text in JSON is done through quoted-string which contains the value in
key-value mapping within { }.
It is similar to the dictionary in Python.
import json

data = '''{ jsonEg1.py


"name" : "Chuck",

"phone" : {
JSON represents data as nested “lists”
"type" : "intl",
and “dictionaries”
"number" : "+1 734 303 4456"

},

"email" : {

"hide" : "yes"

}}'''

info = json.loads(data)

print('Name:',info["name"])

print('Hide:',info["email"]["hide"])
import json
input = '''[
{ "id" : "001",
"x" : "2", jsonEg2.py
"name" : "Chuck"
} , info = json.loads(input)

{ "id" : "009", print('User count:', len(info))


"x" : "7", for item in info:
"name" : "Guido"
print('Name', item['name'])
}
print('Id', item['id'])
]'''
print('Attribute', item['x'])
Web Services - Application Program Interface(API)
The API itself is largely abstract in that it specifies an interface and controls the
behavior of the objects specified in that interface.

The software that provides the functionality described by an API is said to be an


“implementation” of the API.

An API is typically defined in terms of the programming language used to build an


application.
API Security and Rate Limiting
● The compute resources to run these APIs are not “free”
● The data provided by these APIs is usually valuable
● The data providers might limit the number of requests per day, demand an API “key”, or
even charge for usage
● Web Services provide infrastructure for applications cooperating (an API) over a network -
SOAP and REST are two styles of web services

A general rule of thumb when you're deciding between SOAP and REST for building your API:

● If you want standardization and enhanced security, use SOAP.


● If you want flexibility and efficiency, use REST.
● XML and JSON are serialization formats
Data Sources
For JSON

https://mixedanalytics.com/blog/list-actually-free-open-no-auth-needed-apis/

https://jsonplaceholder.typicode.com/

For XML

https://www.w3schools.com/xml/plant_catalog.xml

https://www.hindustantimes.com/rss

You might also like