Chapter-8-converted
Chapter-8-converted
• Boto is a Python package that provides interfaces to Amazon Web Services (AWS)
#Python program for launching an EC2 instance
• In this example, a connection to EC2 service is fi import boto.ec2
rst established by calling from time import sleep
boto.ec2.connect_to_region. ACCESS_KEY="<enter access key>"
SECRET_KEY="<enter secret key>"
• The EC2 region, AWS access key and AWS secret REGION="us-east-1"
key are passed to this function. After connecting AMI_ID = "ami-d0f89fb9"
EC2_KEY_HANDLE = "<enter key handle>"
to EC2 , a new instance is launched using the INSTANCE_TYPE="t1.micro"
conn.run_instances function. SECGROUP_HANDLE="default"
conn =
• The AMI-ID, instance type, EC2 key handle and boto.ec2.connect_to_region(REGION,
security group are passed to this function. aws_access_key_id=ACCESS_KEY,
aws_
secre
t_acc
ess_k
ey=SE
CRET
_KEY)
Book website: http://www.internet-of-things-book.com Bahga & Madisetti, © 2015
Amazon AutoScaling – Python Example
#Python program for creating an AutoScaling group (code excerpt)
• AutoScaling Service import boto.ec2.autoscale
:
• A connection to AutoScaling service is first established by print "Connecting to Autoscaling Service"
calling boto.ec2.autoscale.connect_to_region function. conn = boto.ec2.autoscale.connect_to_region(REGION,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
• Launch Configuration
• After connecting to AutoScaling service, a new launch print "Creating launch configuration"
configuration is created by calling
conn.create_launch_con f iguration. Launch configuration lc = LaunchConfiguration(name='My-Launch-Config-2',
contains instructions on how to launch new instances image_id=AMI_ID,
including the AMI-ID, instance type, security groups, etc. key_name=EC2_KEY_HANDLE,
instance_type=INSTANCE_TYPE,
security_groups = [ SECGROUP_HANDLE, ])
• AutoScaling Group conn.create_launch_configuration(lc)
• After creating a launch configuration, it is then associated print "Creating auto-scaling group"
with a new AutoScaling group. AutoScaling group is
created by calling conn.create_auto_scaling_group. The ag = AutoScalingGroup(group_name='My-Group',
settings for AutoScaling group such as the maximum and availability_zones=['us-east-1b'],
minimum number of instances in the group, the launch launch_config=lc, min_size=1, max_size=2,
configuration, availability zones, optional load balancer to connection=conn)
use with the group, etc. conn.create_auto_scaling_group(ag)
conn.create_scaling_policy(scale_up_po
licy)
conn.create_scaling_policy(scale_down_policy)
conn = boto.connect_s3(aws_access_key_id='<enter>',
aws_secret_access_key='<enter>')
• The example shows inverted index mapper #Inverted Index Mapper in Python
program. #!/usr/bin/env python
import sys
• The map function reads the data from the for line in sys.stdin:
standard input (stdin) and splits the tab-limited doc_id, content = line.split(’’)
words = content.split()
data into document-ID and contents of the for word in words:
document. print ’%s%s’ % (word,
doc_id)
• The map function emits key-value pairs where
key is each word in the document and value is
the document-ID.
• The example shows inverted index reducer #Inverted Index Reducer in Python
program. #!/usr/bin/env python
import sys
• The key-value pairs emitted by the map phase current_word = None
are shuffled to the reducers and grouped by the current_docids = []
word = None
key.
for line in sys.stdin:
• The reducer reads the key-value pairs grouped # remove leading and trailing whitespace
by the same key from the standard input (stdin) line = line.strip()
# parse the input we got from mapper.py
and creates a list of document-IDs in which the word, doc_id = line.split(’’)
word occurs. if current_word == word:
current_docids.append(doc_id)
• The output of reducer contains key value pairs else:
if current_word:
where key is a unique word and value is the list print ’%s%s’ % (current_word, current_docids)
of document-IDs in which the word occurs. current_docids = []
current_docids.append(doc_id)
current_word = word
• JSON
• JavaScript Object Notation (JSON) is an easy to read and write data-interchange format. JSON is used as an alternative to XML
and is is easy for machines to parse and generate. JSON is built on two structures - a collection of name-value pairs (e.g. a
Python dictionary) and ordered lists of values (e.g.. a Python list).
• XML
• XML (Extensible Markup Language) is a data format for structured document interchange. The Python minidom library
provides a minimal implementation of the Document Object Model interface and has an API similar to that in other languages.
• HTTPLib & URLLib
• HTTPLib2 and URLLib2 are Python libraries used in network/internet programming
• SMTPLib
• Simple Mail Transfer Protocol (SMTP) is a protocol which handles sending email and routing e-mail between mail servers. The
Python smtplib module provides an SMTP client session object that can be used to send email.
• NumPy
• NumPy is a package for scientific computing in Python. NumPy provides support for large multi-dimensional arrays and
matrices
• Scikit-learn
• Scikit-learn is an open source machine learning library for Python that provides implementations of various machine learning
algorithms for classification, clustering, regression and dimension reduction problems.
• Django is an open source web application framework for developing web applications in Python.
• A web application framework in general is a collection of solutions, packages and best practices
that allows development of web applications and dynamic websites.
• Django is based on the Model-Template-View architecture and provides a separation of the data
model from the business rules and the user interface.
• Django provides a unified API to a database backend.
• Thus web applications built with Django can work with different databases without requiring any
code changes.
• With this fiexibility in web application design combined with the powerful capabilities of the Python
language and the Python ecosystem, Django is best suited for cloud applications.
• Django consists of an object-relational mapper, a web templating system and a regular-expression-
based URL dispatcher.
• Model
• The model acts as a definition of some stored data and handles the interactions with the database. In a web
application, the data can be stored in a relational database, non-relational database, an XML file, etc. A Django model
is a Python class that outlines the variables and methods for a particular type of data.
• Template
• In a typical Django web application, the template is simply an HTML page with a few extra placeholders. Django’s
template language can be used to create various forms of text files (XML, email, CSS, Javascript, CSV, etc.)
• View
• The view ties the model to the template. The view is where you write the code that actually generates the web pages.
View determines what data is to be displayed, retrieves the data from the database and passes the data to the
template.