IOT UNIT 2 Part 1
IOT UNIT 2 Part 1
Part 1
• Python packages:
• Json
• Httplib
• Urllib
• Smtplib
• Rpio.gpio
JSON
• JSON stands for JavaScript Object Notation
• It is a data format/syntax for store and
exchange data between the browser and the
server.
• It is a complete language-independent text
format.
• It is is supported by all modern browsers
JSON
• JSON is a lightweight data format for data
interchange which can be easily read and
written by humans, easily parsed and
generated by machines.
• For this reason it has replaced XML notation
on many platforms.
XML
• XML is fantastic at describing object
hierarchies and even semantics, but adds a
great deal of overhead to the serialized object
JSON
<xml>
<user>
<firstName>Jason</firstName> <middleName>Alexander</middleName>
<lastName>Smith</lastName>
<address>
<street1>1234 Someplace Avenue</street1> <street2>Apt. 302</street2>
<city>Anytown</city>
<state>NY</state>
<postalCode>12345</postalCode> <country>US</country>
</address>
</user>
</xml>
JSON
{
"firstName" : "Jason", "middleName" :
"Alexander", "lastName" : "Smith",
"address" : {
"street1" : "1234 Someplace Avenue", "street2" :
"Apt. 302", "city" : "Anytown", "state" : "NY",
"postalCode" : "12345", "country" : "US"
}
}
Referencing Values within JSON Object
Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null
Serialization
• The process of encoding JSON is usually
called serialization. This term refers to the
transformation of data into a series of bytes (hence
serial) to be stored or transmitted across a network.
To handle the data flow in a file, the JSON library in
Python uses dump() function to convert the Python
objects into their respective JSON object, so it makes
it easy to write data to files. See the following table
given below.
Deserialization
• is the opposite of Serialization, i.e. conversion
of JSON objects into their respective Python
objects. The load() method is used for it.
• If you have used JSON data from another
program or obtained it as a string format of
JSON, then it can easily be deserialized with
load(), which is usually used to load from a
string, otherwise, the root object is in a list or
dict.
• Convert from JSON to Python
• Example
• import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
import json
person_dict = {"name": "Bob", "languages":
["English", "French"], "married": True, "age":
32 }
with open('person.txt', 'w') as json_file:
json.dump(person_dict, json_file)
XML
• XML is an abbreviation name of "Extensible
Markup Language".
• It was designed to store and transport small to
medium amounts of data and is widely used
for sharing structured information.
• It is primarily focused on creating web pages
where the data has a specific structure.
XML
• import xml.etree.ElementTree as ET
• tree = ET.parse('items.xml')
• root = tree.getroot()
• # all items data print('Expertise Data:')
• for elem in root:
for subelem in elem:
print(subelem.text)
output
Expertise Data:
SQL
Python
httplib
• Httplib2 and urllib2 are python libraries used in
network/internet programming.
• The httplib module in python version 2 has been
renamed to http.client in python 3.
• The httplib and http.client both module defines
classes which implement the client side of the HTTP
and HTTPS protocols.
• If you are using python version 2, install httplib module.
• 2.If you are using python version 3, install
http.client module.
• We use the pip command to install the necessary
modules.
• Installing httplib
• pip install httplib
• Installing http.client
• pip install http.client
Methods of httplib
• GET
• POST
• PUT
GET method
Here is an example session that uses the GET method:
import urllib.request
import urllib.parse
# trying to read the URL
try:
x = urllib.request.urlopen('https://www.google.com / search?q = test')
print(x.read())
# Catching the exception generated
except Exception as e :
print(str(e))
SMTPlib
• Simple Mail Transfer Protocol (SMTP) is a
communication protocol for electronic mail
transmission.
• The smtplib is a Python library for sending
emails using the Simple Mail Transfer Protocol
(SMTP).
SMTP
• smtplib module defines an the SMTP client
session object
• The SMTP object is used for the email transfer.
syntax to create the smtplib object
• import smtplib
• smtpObj = smtplib.SMTP(host, port, local_host
name)
smtplib
• The sendmail() method of the SMTP object is
used to send the mail to the desired machine.
The syntax is given below.
• smtpObj.sendmail(sender, receiver, message)
Example
import smtplib
sender_mail = '[email protected]'
receivers_mail = ['[email protected]']
message = """From: From Person %s
To: To Person %s
Subject: Sending SMTP e-mail
This is a test e-mail message.
"""%(sender_mail,receivers_mail)
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender_mail, receivers_mail, message)
print("Successfully sent email")
except Exception:
print("Error: unable to send email")
Sending email from gmail