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

6.Python Scripting

This document provides an introduction to Python scripting, highlighting its features such as being high-level, interpreted, and object-oriented. It covers installation instructions for both Windows and Linux, basic syntax, variable types, and data structures like strings, lists, tuples, and dictionaries. The document emphasizes Python's readability and versatility for automation and programming tasks.

Uploaded by

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

6.Python Scripting

This document provides an introduction to Python scripting, highlighting its features such as being high-level, interpreted, and object-oriented. It covers installation instructions for both Windows and Linux, basic syntax, variable types, and data structures like strings, lists, tuples, and dictionaries. The document emphasizes Python's readability and versatility for automation and programming tasks.

Uploaded by

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

VI Python Scripting.

1. Python Introduction
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is
designed to be highly readable.
So why is python tutorial in this book?
When we were writing bash script for doing automation, slowly we increased the complexity of our
code. Our code became more and more complex. Also, we do not have any other feature in bash
scripting apart from just automating Linux tasks.
Python is among the easiest programming language out there.
Being easy it gives us lots of feature and libraries which extends the power of python. Python is
very extensible. We can use python to run Linux automation, windows automation, Cloud
automation and several others. It’s very versatile in nature.

Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to
compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter,
directly to write your programs.
Python is Object-Oriented: Python supports Object-Oriented style or technique of programming
that encapsulates code within objects.

Python is a Beginner's Language: Python is a great language for the beginner-level programmers
and supports the development of a wide range of applications from simple text processing to WWW
browsers to games.

Installing python.

Windows Installation
Here are the steps to install Python on Windows machine.
Open a Web browser and go to http://www.python.org/download/. Follow the link for the Windows
installer python-XYZ.msi file where XYZ is the version you need to install.
To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0.
Save the installer file to your local machine and then run it to find out if your machine supports
MSI.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just
accept the default settings, wait until the install is finished, and you are done.
After installation we have to setup Python PATH in the system environment variable.

Linux Installation
Linux system by default comes with python installed so it is not required to take any action for
Linux system.

2. Basic Syntax

Python Interactive programming


Open Linux shell => Type python and hit enter, this will drop you into the python shell/interpreter.

Type the following text at the Python prompt and press the Enter:

Python scripting
Create a file named hello.py, py stands for python and is extension of python script file.
Add below mentioned content and save file.
#!/usr/bin/python

print “Hello, DevOps!”

Line 1- #! is the shebang charect as we know from bash scripting, Interpreter path is
/usr/bin/python. You can find python interpreter path in your Linux system by giving “which
python”. Invoking the interpreter with a script parameter begins execution of the script and
continues until the script is finished. When the script is finished, the interpreter is no longer active.

Line 2- print is the python command which prints messages.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Executing python script.
Now, try to run this program as follows:
$ python hello.py
or make the hello.py executable by giving command “chmod u+x hello.py” and execute as below.
$ ./hello.py
This produces the following result:
Hello, DevOps!

Lines and Indentation


Beware of spaces!
Python program statements are written one above the other, as shown in the following example:
print “Python is a scripting language.”

print "We are using it to learn automation."

print "Python is very extensible."

The 3 line program above is correct and will run producing the expected output.
The program below will generate a syntax error
because of the space character inserted at the start of the second line.
print “Python is a scripting language.”

print "We are using it to learn automation."

print "Python is very extensible."

Python evaluate the second print statement as a block of code inside first print statement. So its
thinking seond print is under first print statement but its a separate statement.
Python provides no braces to indicate blocks of code for class and function definitions
or flow control. Blocks of code are denoted by line indentation, which is rigidly
enforced.

The if statement
The if statement starts with the keyword "if"

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
followed by a test case (e.g. x == 30), followed by a colon (:).
x = 30

if x == 30:

print ‘x is not equal to 30’

else:

print ‘x is not equal to 30’

Under the if statement, the statements to be run if the condition is true are entered.
after typing a few space characters. In the example above, two space characters are used.
Any statements to be run after the else: part of the program also have to be spaced out to the
***same level*** by typing 2 space characters before the statement.

Spacing out a program in this way is known as indenting.


Indentation is part of the syntax of the Python language.

The number of spaces in the indentation is variable, but all statements within the block must be
indented the same amount. For example:
if True:

print "True"

else:

print "False"

However, the following block generates an error:


if True:

print "Answer"

print "True"

else:

print "Answer"

print "False"

Thus, in Python all the continuous lines indented with same number of spaces would
form a block.
Note: Do not try to understand the logic at this point of time. Just make sure you
understood various blocks even if they are without braces.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Quotation in Python
We can use quotes in python for enclosing the text/strings. There are three types of Quotes in
python.

• Single Quote (')


Anything inside single quotes is consider as literal string and will not be evaluated by
python.

• Double (")
Double quotes can be used when you have a variable in the string that needs to be evaluated
by python using string formating(%).

• Triple (''' or """)


This is used to write a multiline string or also called as paragraph string.

For example:
word = 'word'

sentence = "This is a sentence."

paragraph = """This is a paragraph. It is

made up of multiple lines and sentences."""

Comments in Python
A hash sign (#) is a comment symbol in python. Python ignores all the text after # and does not
evaluate it. All characters after the # and up to the end of the physical line are part of the comment
and the
Python interpreter ignores them.
#!/usr/bin/python

# First comment

print "Hello, Python!";

# second comment

This produces the following result:


Hello, Python!

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
You can type a comment on the same line after a statement or expression:
name = "fuzi island" # This is a comment

You can comment multiple lines as follows:


# This is a comment.

# This is a comment, too.

# This is a comment, too.

# I said that already.

Multiline Comment.

We can use paragraph string to create a multiline comment. Everything inside paragraph string
would be ignored by Python.
#!/usr/bin/python

num = 84

"""This is multiline comment,

Anything enclosed in this will not be evaluated

by python"""

3. Variable Types
Variable are temporary storage for data in memory. There are different kinds of data, by assigning
different data types to variables, you can store integers, decimals, or characters in these variables.

Variable Assignment.
We don’t need to define variable types in python like other programming language. Variable
declaration happens automatically when we assign a value to a variable.
The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of
the = operator is the value stored in the variable.

For example:
#!/usr/bin/python

mynumber = 86 # A integer variable assignment

myfloatnumber = 86. 11 # A float variable assignment

myname = “imran” # A string variable assignment

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
print mynumber

print myfloatnumber

print myname

Here 86. 86.11 & imran are values assigned to variables names mynumber, myfloatnumber &
myname respectively. Execute the above script and check the output.
Multiple Assignment
Python allows you to assign a single value to several variables simultaneously. For
example:
x = y = z= 86

Here, an integer object is created with the value 86, and all three variables are assigned to the same
memory location.

You can also assign multiple objects to multiple variables.


For example:
x,y,z = 86, 11, “imran”

Here, two integer objects with values 86 and 11 are assigned to variables x and y respectively, and
one string object with the value "imran" is assigned to the variable z.

Python Data Types


The data stored in memory can be of many types. For example, a person's age is stored as a numeric
value and his or her address is stored as alphanumeric characters. Python has various standard data
types that are used to define the operations possible on them and the storage method for each of
them.
Python has five standard data types:

• Numbers

• String

• List

• Tuple

• Dictionary

Strings

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Python strings are simple text data enclosed in single or double quotes
stringvar = ‘sample string’

strvar2 = ‘string#2’

Python allows for either pairs of single or double quotes.


stringvar = “sample string”

strvar2 = “string#2”

String slicing can also be done and stored into a variable, slice means subset or part of the string.
Subsets of strings can be done by slice operator ([ ] and [:] ) with indexes starting at
0 in the beginning of the string and working their way from -1 at the end.
The plus (+) sign is the string concatenation operator and the asterisk (*) is the
repetition operator.
For example:

#!/usr/bin/python

teststr = 'Hello DevOps!'

print teststr # Prints complete string

print teststr[0] # Prints first character of the string

print teststr[0:5] # Prints characters starting from 0th to 5th

print teststr[6:12] # Prints characters starting from 6th to 12th

print teststr[3:] # Prints string starting from 3rd character

print teststr * 2 # Prints string two times

print teststr + " Hola" # Prints concatenated string

This will produce the following result:

Hello DevOps!

Hello

DevOps

llo DevOps!

Hello DevOps!Hello DevOps!

Hello DevOps! Hola

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Lists
Lists are also knows a array of python. It can store multiple values of diffrent datatype into a
variable. A list contains items separated by commas and enclosed within square brackets ([]). Its
similar to arrays in C but difference between them is that all the items belonging to a list can be of
different data type.
The values stored in a list can be accessed using the slice operator ([ ] and [:]) with
indexes starting at 0 in the beginning of the list and working their way to end -1. The
plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition
operator.

For example:
#!/usr/bin/python

list1 = [ 'ansible', 1111 , 'puppet', 86.96, 'git', 'aws']

list2 = ['miner', 'boy']

print list1 # Prints complete list

print list1[0] # Prints first element of the list

print list1[-1] # Prints last element of the list

print list1[2:4] # Prints elements starting from 2nd till 4th

print list1[3:] # Prints elements starting from 3rd element

print list2 * 2 # Prints list two times

This produces the following result:


['ansible', 1111, 'puppet', 86.96, 'git', 'aws']

ansible

aws

['puppet', 86.96]

[86.96, 'git', 'aws']

['miner', 'boy', 'miner', 'boy']

['ansible', 1111, 'puppet', 86.96, 'git', 'aws', 'miner', 'boy']

Tuples

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Tuple is another type of array it looks and feel exactly like a list but tuple are immutable that means
you can’t edit a tuple just like you can’t edit the content in a cd rom. A tuple consists of a
number of values separated by commas. Tuples are enclosed in parenthesis ().Tuples can be thought
of as read-only lists. For example:
#!/usr/bin/python

tuple1 = ( 'ansible', 1111, 'puppet', 86.96, 'git', 'aws')

tuple2 = ('miner', 'boy')

print tuple1 # Prints complete list

print tuple1[0] # Prints first element of the list

print tuple1[1:3] # Prints elements starting from 2nd till 3rd

print tuple1[2:] # Prints elements starting from 3rd element

print tuple2 * 2 # Prints list two times

print tuple1 + tuple2 # Prints concatenated lists

This produces the following result:


('ansible', 1111, 'puppet', 86.96, 'git', 'aws')

ansible

(1111, 'puppet')

('puppet', 86.96, 'git', 'aws')

('miner', 'boy', 'miner', 'boy')

('ansible', 1111, 'puppet', 86.96, 'git', 'aws', 'miner', 'boy')

The following code is invalid with tuple, because we attempted to update a tuple,
which is not allowed. Similar case is possible with lists:
#!/usr/bin/python

tuple = ('ansible', 1111, 'puppet', 86.96, 'git', 'aws'

list = ['ansible', 1111, 'puppet', 86.96, 'git', 'aws']

tuple[5] = ‘terra’ # Invalid syntax with tuple

list[5] = ‘terra’ # Valid syntax with list

Dictionary
In dictionary data in stored in format of key-value pair unlike list or tuple where we just have
values. You can think it a any normal english dictionary where word in the key and its meaning is
its value.
Python's dictionaries are kind of hash table type. A dictionary key can be almost any Python type,
but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.
Visualpath Training & Consulting.
Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using
square braces ([]).
For example:
#!/usr/bin/python

dict1 = {}

dict1['profile'] = "hitman"

dict1['passcode'] = "zeus"

dict2 = {'name': 'imran','code':47, 'dept': 'hitman'}

print dict1['profile'] # Prints value for 'profile' key

print dict1['passcode'] # Prints value for 'passcode' key

print dict2 # Prints complete dictionary

print dict2.keys() # Prints all the keys

print dict2.values() # Prints all the values

This produces the following result:


hitman

zeus

{'dept': 'hitman', 'code': 47, 'name': 'imran'}

['dept', 'code', 'name']

['hitman', 47, 'imran']

Dictionaries have no concept of order among elements. It is incorrect to say that the
elements are "out of order"; they are simply unordered.

4. Python Operators
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called
operator.
Types of Operators
Python language supports the following types of operators.

• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators

Python Arithmetic Operators

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Assume variable x holds 50 and variable y holds 60, then:

+ Addition Adds values on either side of the operator.


x + y = 110

- Subtraction Subtracts right hand operand from left hand


operand.
x – y = -10

* Multiplication Multiplies values on either side of the operator


x * y = 200

/ Division Divides left hand operand by right hand operand


x/y=2

% Modulus Divides left hand operand by right hand


operand and returns remainder
x%y=0

** Exponent Performs exponential (power) calculation on


operators
x**y =10 to the
power 20

Python Comparison Operators


These operators compare the values on either sides of them and decide the relation
among them. They are also called Relational operators.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Example
#!/usr/bin/python

a = 2

b = 30

if ( a == b ):

print "a is equal to b"

else:

print "a is not equal to b"

if ( a != b ):

print "a is not equal to b"

else:

print "a is equal to b"

if ( a < b ):

print "a is less than b"

else:

print "a is not less than b"

if ( a > b ):

print "a is greater than b"

else:

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
print "a is not greater than b"

Once executed it return below output.

a is not equal to b
a is not equal to b
a is less than b
a is not greater than b

Python Logical Operators

There are following logical operators supported by Python language.


If variable a holds 40 and variable b holds 50 then:
Operator Description Example

If both the operands are true then condition


becomes true. (a and b) is true.

If any of the two operands are non-zero


then condition becomes true. (a or b) is true.

Used to reverse the logical state of its


operand. Not (a and b) is false.

Python Membership Operators

Python’s membership operators test for membership in a sequence, such as strings,


lists, or tuples. There are two membership operators as explained below:

Evaluates to true if it finds a variable in


the specified sequence and false
otherwise.

Example
Visualpath Training & Consulting.
Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
#!/usr/bin/python

a = 10

b = 20

list = [1, 2, 3, 4, 5 ];

if ( a in list ):

print "a is available in the given list"

else:

print "a is not available in the given list"

if ( b not in list ):

print "b is not available in the given list"

else:

print "b is available in the given list"

a = 2

if ( a in list ):

print "a is available in the given list"

else:

When executed gives below result.

a is not available in the given list

b is not available in the given list

a is available in the given list

5. Decision Making

Decision making is there in every programming language. Based on a test case the program decides
to execute statements/commands or not. In python we use if, elif and else keywords to make
decisions.
Syntax

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
if test case:

code

If the test case is true then the code in if block gets executed.
If the text expression is false, the statement(s) is not executed.As we have seen previously in
indentation section to create a block of code underneath any statement we indent the code or we
give spaces to it.
For example, the print statement comes under the if block.
if True:

print “Indented three spaces”

In python 0 is interpreted as False and any non zero value is True.


Python if Statement Flowchart

#!/usr/bin/python

# If the number is
positive, we print an
appropriate message

num = 30

if num == 30:

print "Number is equal to 30."

print "This is always printed. Rest of the code."

num = 82

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
if num < 100:

print num, "is less than 100."

print "This is also always printed.Rest of the code."

Python if/else Statement

Syntax of if/else
if test case:

Body of if

else:

Body of else

The if/else statement evaluates test case and will execute body of if only when test condition is true.
If the condition is false body of else is executed.
Python if. Else Flowchart

#!/usr/bin/python

# Program checks
if the number is
positive or negative

# And displays an appropriate message

num = -4

if num >= 0:

print "Positive or Zero"

else:

print "Negative number"

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Python if/elif/else

Syntax of if/elif/else
if test case:

Body of if

elif test case:

Body of elif

else:

Body of else

If the condition is false else block gets executed but what if we want to evaluate few more condition
and then decide the execution. We can use elif to put more condtions in out decision making
process its short form of else if.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, body of else is executed.

The if block can have only one else block. But it can have multiple elif blocks.

Flowchart of if...elif...else

#!/usr/bin/python

# In this program,

# we check if the number is greater than or

# less than 10

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
num = 84

if num > 10:

print "Greater than 10"

elif num < 10:

print "Less than 10"

else:

print "Value out of range"

6. Loops
Loops are used when you have a block of code that you want to repeat it for fixed number of time
or until a condition is not satisfied.
Python provides us with two loops
While loop: Repeat the block of code untill the while condition is true.
while test_case:

Body of while

For loop: Repeat the block of code for a number of time.


for variable in sequence:

Body of for

For loops are traditionally used when you have a block of code which you want to repeat a fixed
number of times. The Python for statement iterates over the members of a sequence in order,
executing the block each time. Contrast the for statement with the ''while'' loop, used when a
condition needs to be checked each iteration, or to repeat a block of code forever. For example:

For loop from 0 to 2, therefore running 3 times.


for x in range(0, 3):

print "We're on time %d" % (x)

While loop from 1 to infinity, therefore running forever.


x = 1

while True:

print "To infinity and beyond! We're getting close, on %d now!" % (x)

x += 1

While Loops

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
In while loop first the test case is evaluated, if the test case is True then the body of while loop gets
executed and repeated untill the test case evaluates to false. After every iteration the test case is
evaluated.
Python interprets any non-zero value as True. None and 0 are interpreted as False.

Flowchart of while Loop


Example
#!/usr/bin/python

count = 0

while (count < 12):

print 'The count is:', count

count = count + 1

print "Good bye!"

For loops
For loop can iterate over any sequences like list, tuple or string which are indexed. Every element in
these datatypes are indexed can be iterated over. This process of iterating over a sequence is also
called as traversing or traversal in nature.

for variable1 in sequence:

Body of for

Here, variable1 is the variable that takes the value of the item inside the sequence on each iteration.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from
the rest of the code using indentation.
Flowchart of for Loop

#!/usr/bin/python

for word in 'DevOps!': # First Example

print 'Current Word :', word

planets = ['Mercury', 'Venus',’Earth’]

for planet in planets: # Second Example

print 'Current planet :', planet

print “Bye!”

Iterating over range, range and len function.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
range() function return a list and for loop can iterate over a list.
#!/usr/bin/python

fruits = ['banana', 'apple', 'mango']

for index in range(len(fruits)):

print 'Current fruit :', fruits[index]

print "Good bye!"

Break and Continue


Using for loops and while loops in Python allow you to automate and repeat tasks in an efficient
manner.
But sometimes you wish to interept or change the flow or exit from the loop while its executing.
In Python, break and continue statements can alter the flow of a normal loop.
The break and continue statements are used in these cases.

Python break statement


The break statement provides you with the opportunity to exit out of a loop when an external
condition is triggered, this external condition could be if block which evaluates to true somehere in
the iteration and use break keyword to exit from the loop.

Flowchart of break

The working of break statement in for loop and while loop is shown below.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
#!/usr/bin/python

for word in 'Python':

# First Example

if word == 't':

break

print 'Current Letter :', word

a = 10

# Second Example

while a > 0:

print 'Current variable value :', a

a = a -1

if a == 5:

break

print "Execution completed!"

Current Letter : P

Current Letter : y

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Current variable value : 10

Current variable value : 9

Current variable value : 8

Current variable value : 7

Current variable value : 6

Execution completed!

Python continue statement


The continue statement is used to skip the rest of the code inside a loop for the current iteration
only. Loop does not terminate but continues on with the next iteration.

Flowchart of continue

The working of continue statement in for and while loop is shown below.

#!/usr/bin/python

for word in 'Python':

# First Example

if word == 't':

continue

print 'Current Letter :', word

a = 10

# Second Example

while a > 0:

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
a = a -1

if a == 5:

continue

print 'Current variable value :', a

print "Execution completed!"

Current Letter : P

Current Letter : y

Current Letter : h

Current Letter : o

Current Letter : n

Current variable value : 9

Current variable value : 8

Current variable value : 7

Current variable value : 6

Current variable value : 4

Current variable value : 3

Current variable value : 2

Current variable value : 1

Current variable value : 0

Execution completed!

7. Built In Methods/Functions

Python has numerous built in methods that extends its capabilities. Listed below are few of the
useful built in methods on all the DataType.
Check Python docs for the list of all the methods and its uses.

String

Capitalize: Capitalizes first letter of string.

str = "this is string example....wow!!!";

print "str.capitalize() : ", str.capitalize()

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
find: Determine if str occurs in string or in a substring.
str1 = "this is string example....wow!!!";

str2 = "exam";

print str1.find(str2);

print str1.find(str2, 10);

print str1.find(str2, 40);

join: Merges (concatenates) the string representations of elements in


sequence seq into a string, with separator string.
str = "-";

seq = ("a", "b", "c"); # This is sequence of strings.

print str.join( seq );

rstrip: Removes all trailing whitespace of string.


a = “Hello ”

a.rstrip()

Hello

split: Splits string according to delimiter str (space if not provided) and
returns list of substrings; split into at most num substrings if given.
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";

print str.split( );

['Line1-abcdef', 'Line2-abc', 'Line4-abcd']

Lists

append: Adds element at the end of the list


aList = [123, 'xyz', 'zara', 'abc'];

aList.append( 2009 );

print "Updated List : ", aList;

extend: Combine two list together


aList = [123, 'xyz', 'zara', 'abc', 123];

bList = [2009, 'manni'];

aList.extend(bList)

print "Extended List : ", aList ;

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
insert: Insert a element into the specified index value.
aList = [123, 'xyz', 'zara', 'abc']

aList.insert( 3, 2009)

print "Final List : ", aList

pop: Removes element from list at a specified index value, default is last element.
aList = [123, 'xyz', 'zara', 'abc'];

print "A List : ", aList.pop();

print "B List : ", aList.pop(2);

Dictionary

Update dictionary elements


dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

dict['Age'] = 8; # update existing entry

dict['School'] = "DPS School"; # Add new entry

print "dict['Age']: ", dict['Age'];

print "dict['School']: ", dict['School'];

delete dictionary elements


dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

del dict['Name']; # remove entry with key 'Name'

dict.clear(); # remove all entries in dict

del dict ; # delete entire dictionary

print "dict['Age']: ", dict['Age'];

print "dict['School']: ", dict['School'];

8. Functions
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing.

We have already used some built in functions like range(), len(), type() etc. We will learn now how
to write our own functions.

Functions rules
Function block begins with the keyword def followed by function name and parantheses.
Syntax
def func_name()

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
If any arguments has to passed to the function it should be defined under paranthesis.
Syntax
def func_name(arg1, arg2, arg3)

Code block starts with a colon :


Syntax
def func_name(arg1,arg2):

code block

Keyword “return” in the code block exits the function and can return some value, if no value is
returned default return value is “None”.
Syntax
def func_name(arg1,arg2):

code block

return variable

def func_name(arg1,arg2):

code block

return # returns value as None

Defining & Calling a function.


#!/usr/bin/python

def print_func( var1 ):

"This prints a passed string into this function"

print var1

return

# Calling print_func

print_func("Testing function calling.")

# Printing & Calling print_func

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
print print_func("Testing function calling.")

# Storing print_func returned value

returnedvalue = print_func("Testing function calling.")

print returnedvalue

Output
Testing function calling.

Testing function calling.

None

Testing function calling.

None

Function Arguments
You can call a function by using the following types of formal arguments:

• Required arguments

• Keyword arguments

• Default arguments

• Variable-length arguments

Required Arguments
Required arguments are the arguments passed to a function in correct positional order. Here, the
number of arguments in the function call should match exactly with the function definition.
To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax
error as follows:
#!/usr/bin/python

# Function definition is here

def printme( str ):

"This prints a passed string into this function"

print str;

return;

# Now you can call printme function

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
printme();

When the above code is executed, it produces the following result:


Traceback (most recent call last):

File "test.py", line 11, in <module>

printme();

TypeError: printme() takes exactly 1 argument (0 given)

Keyword Arguments
Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name.
This allows you to skip arguments or place them out of order because the Python interpreter is able
to use the keywords provided to match the values with parameters.
You can also make keyword calls to the printme() function in the following ways
#!/usr/bin/python

# Function definition is here

def printme( str ):

"This prints a passed string into this function"

print str;

return;

# Now you can call printme function

printme( str = "My string");

When the above code is executed, it produces the following result:


My string

The following example gives more clear picture. Note that the order of parameters
does not matter.
#!/usr/bin/python

# Function definition is here

def printinfo( name, age ):

"This prints a passed info into this function"

print "Name: ", name;

print "Age ", age;

return;

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
# Now you can call printinfo function

printinfo( age=50, name="miki" );

When the above code is executed, it produces the following result:


Name:

Age

miki

50

Default Arguments
A default argument is an argument that assumes a default value if a value is not
provided in the function call for that argument. The following example gives an idea
on default arguments, it prints default age if it is not passed:
#!/usr/bin/python

# Function definition is here

def printinfo( name, age = 35 ):

"This prints a passed info into this function"

print "Name: ", name;

print "Age ", age;

return;

# Now you can call printinfo function

printinfo( age=50, name="miki" );

printinfo( name="miki" );

When the above code is executed, it produces the following result:


Name:

Age

50

Name:

Age

miki

miki

35

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Variable Length Arguments
You may need to process a function for more arguments than you specified while defining the
function. These arguments are called variable-length arguments and are not named in the function
definition, unlike required and default arguments.
Syntax for a function with non-keyword variable arguments is this:

def functionname([formal_args,] *var_args_tuple ):

"function_docstring"

function_suite

return [expression]

An asterisk (*) is placed before the variable name that holds the values of all non keyword variable
arguments. This tuple remains empty if no additional arguments are specified during the function
call. Following is a simple example:
#!/usr/bin/python

# Function definition is here

def printinfo( arg1, *vartuple ):

"This prints a variable passed arguments"

print "Output is: "

print arg1

for var in vartuple:

print var

return;

# Now you can call printinfo function

printinfo( 10 );

printinfo( 70, 60, 50 );

When the above code is executed, it produces the following result:


Output is:

10

Output is:

70

60

50

The return Statement

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
The statement return [expression] exits a function, optionally passing back an expression to the
caller. A return statement with no arguments is the same as return None. All the above examples are
not returning any value. You can return a value from function as follows:
#!/usr/bin/python

# Function definition is here

def sum( arg1, arg2 ):

# Add both the parameters and return them."

total = arg1 + arg2

print "Inside the function : ", total

return total;

# Now you can call sum function

total = sum( 10, 20 );

print "Outside the function : ", total

When the above code is executed, it produces the following result:


Inside the function : 30

Outside the function : 30

9. Modules
Modules in Python are simply Python files with the .py extension, which implement a set of
functions. Modules are imported from other modules using the import command.

We use modules to break down large programs into small manageable and organized files.
Furthermore, modules provide reusability of code.
We can define our most used functions in a module and import it, instead of copying their
definitions into different programs.
Example script name is modexa.py
imran@DevOps:/tmp$ cat modexa.py

def multi(a, b):

c = a * b

return c

def hello():

print "Hello from modexa module."

Importing a module and calling methods inside it.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
The from...import Statement
Python's from statement lets you import specific attributes from a module into the
current namespace. The from...import has the following syntax:
from modname import name1[, name2[, ... nameN]]

For example, to import the function fibonacci from the module fib, use the following statement:
from fib import fibonacci

This statement does not import the entire module fib into the current namespace; it just introduces
the item fibonacci from the module fib into the global symbol table of the importing module.

The from...import * Statement:


It is also possible to import all names from a module into the current namespace by using the
following import statement:
from modname import *

This provides an easy way to import all the items from a module into the current

The dir( ) Function


The dir() built-in function returns a sorted list of strings containing the names defined by a module.
The list contains the names of all the modules, variables and functions that are defined in a module.
Following is a simple example:

#!/usr/bin/python
# Import built-in module math
import math
content = dir(math)
print content;

When the above code is executed, it produces the following result:

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan',


'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp',
Visualpath Training & Consulting.
Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

Here, the special string variable __name__ is the module's name, and __file__ is the.
filename from which the module was loaded.

10. Python for OS tasks.

Python is very widely used as a scripting language to automate day to day system admin tasks or
even a full scale Orchestration of multiple systems.
The OS module in Python provides a way of using operating system dependent functionality.
The functions that the OS module provides allows you to interface with the underlying operating
system that Python is running on. (Windows, Mac or Linux.
You can find important information about your location or about the process.
Before we start, make sure that you have imported the OS module "import os"

OS Functions explained`
os.system(“ls”) # Executing a shell command

os.chdir() # Move focus to a different directory

os.getcwd() # Returns the current working directory

os.getpid() # Returns the real process ID of the current process

os.chmod() # Change the mode of path to the numeric mode

os.chown() # Change the owner and group id

os.getsize() # Get the size of a file

os.uname() # Return information about the current operating system

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
os.listdir(path)# List of the entries in the directory given by path

os.path.exists()# Check if a path exists

os.mkdir(path) # Create a directory named path with numeric mode mode

os.remove(path) # Remove (delete) the file path

os.rmdir(path) # Remove (delete) the directory path

os.makedirs(path)# Recursive directory creation function

os.removedirs(path) # Remove directories recursively

os.rename(src, dst) # Rename the file or directory src to dst

OS Functions Examples

Check for directory or file.

path = "/tmp/pysys"

if os.path.isdir(path):

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
print "It is a directory."

if os.path.isfile(path):

print "It is a file."

Creates a directory
print os.mkdir('devopsdir', 0750)

Remove a directory with os.rmdir()


print os.rmdir('directory')

Rename a file with os.rename()


print os.rename('/path/to/old/file', '/path/to/new/file')

Move focus to a different directory with os.chdir()


print os.chdir('/tmp')

Print out all directories, sub-directories and files with os.walk()


for root, dirs, files in os.walk("/tmp"):

print root

print dirs

print files

Returns a list of all files in a directory with os.listdir()


for filename in os.listdir("/tmp"):

print "This is inside /tmp", filename

Get the size of a file with os.path.getsize()


path.getsize("/tmp/file.txt")

11. Fabric for automation

What is Fabric?
As the README says:

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for
application deployment or systems administration tasks.
More specifically, Fabric is:
A tool that lets you execute arbitrary Python functions via the command line;
A library of subroutines (built on top of a lower-level library) to make executing shell commands
over SSH easy and Pythonic.
Naturally, most users combine these two things, using Fabric to write and execute Python functions,
or tasks, to automate interactions with remote servers

Installing Fabric
Fabric module does not comes inbuilt in python but can be installed with python package manager
like “pip”.pip is a package management system used to install and manage software
packages written in Python.
First we have to install pip.
Do I need to install pip?
pip is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded
from python.org, but you'll need to upgrade pip.

Installing with get-pip.py


To install pip, securely download https://bootstrap.pypa.io/get-pip.py
Then run the following:
python get-pip.py

Installing fabric with pip.


pip install fabric

Fabfile or Fab scripts


Fabric will load fabfile.py and run the functions that we defined in it.
Create a python script named fabfile.py and define functions in it.
For Example:

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Fab commands are executed from bash shell, when fab command is executed it looks for fabfile.py
script locally and call the function inside it.

Task arguments
It’s often useful to pass runtime parameters into your tasks, just as you might during regular Python
programming.

Fabric functions
Fabric provides a set of commands in fabric API that are simple but powerful.

With Fabric, you can use simple Fabric calls like


local # execute a local command)
run # execute a remote command on all specific hosts, user-level permissions)
sudo # sudo a command on the remote server)
put # copy over a local file to a remote destination)
get # download a file from the remote server)
prompt # prompt user with text and return the input (like raw_input))
reboot # reboot the remote system, disconnect, and wait for wait seconds)
To test run, sudo, get, put & reboot functions you will need a remote linux system. Could be a vm
or cloud instance.

from fabric.api import *

def hello(name="world"):

print "Hello", name

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
# Execute ls -ltr command on local machine.

def calling_local():

local("ls -ltr")

# Creates directory, creates files and list them on remote machine's.

def test_run():

run("mkdir -p /tmp/testdir && ls /tmp/testdir")

run("touch /tmp/testdir/file86")

run("ls -ltr /tmp/testdir")

# Installs tree package on remote machine with sudoers privilege.

def test_sudo():

sudo("apt-get install tree")

# Push file devfile4 to a remote machine on path /home/vagrant/ like scp.

def test_put():

put("devfile4", "/home/vagrant/")

# Takes user input & pull file from remote machine to local machine.

def test_get():

filepath=prompt("Enter file path to download")

get(filepath, ".", use_sudo=True)

Calling Fab functions.


When we call fab functions from shell we may or may not need to pass some arguments depending
on what is running inside the fab function. For example if I am calling a local method then it just
runs OS tasks/commands but if I am calling run or sudo method then it needs the remote server IP
and credentials to create an SSH session.
If we have to pass remote server IP and user/pass we use below syntax.
fab -H <IP> -u <username> -p ‘<password>’ functioname

This remote server information can also be added into the fabfile by using env.hosts, env.user and
env.password variables which we will see in next example.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Fabric Helpers.
cd context manager allows keeping the directory state (i.e. where the following block of comments
are to be executed). It is similar to running the cd command during an SSH session and running
various different commands.
Usage examples:
# executing command from a particualr directory.

with cd("/tmp/"):

items = sudo("ls -l")

The lcd context manager (local cd) works very similarly to one above (cd); however, it only
affects the local system's state.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Usage examples:
# Change the local working directory to /opt/nexus

# and upload a file

with lcd("/opt/nexus"):

print "Uploading the project archive"

put("nexus.tar.gz", "/tmp/nexus.tar.gz")

Check Fab Documentation for more helpers.


http://docs.fabfile.org/en/1.13/api/core/context_managers.html

Example Fabfile for automating apache setup


In this fabfile we will write functions to setup apache and also cleaning apache setup on a remote
machine. We will use env.hosts, env.user and env.password variables to define remote server
information. env.hosts is a list and we can add n number of IP/hostnames in the list separated by a
comma.

from fabric.api import *

env.hosts=['192.168.1.9']

env.user='vagrant'

env.password='vagrant'

def apache_install():

sudo("apt-get install apache2 -y")

def apache_start():

sudo("service apache2 start")

def apache_enable():

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
sudo("sudo update-rc.d apache2 enable")

def push_index():

put("index.html", "/var/www/index.html", use_sudo=True)

def apache_restart():

sudo("service apache2 restart")

def disable_firewall():

sudo("service ufw stop")

# Calling all the functions defined above from a single function.

def assemble_apache():

apache_install()

apache_start()

apache_enable()

push_index()

apache_restart()

disable_firewall()

# Define all the fabric methods to clean apache setup in just one function.

def dismantle_apache():

sudo("service apache2 stop")

sudo("apt-get remove apache2 -y")

sudo("sudo update-rc.d apache2 disable")

sudo("service ufw start")

From the above script, we have seen that initially while doing setup we have given only one fabric
method in one function. But while dismantling/cleaning apache we have given all the required
fabric method in just one function. Both the approach works fine but if I have separate function to
start or install apache or setup firewall rule I can also call them individually from bash shell. This is
very convenient if I have just start apache on an array of remote host I already have a separate
function defined for it. Like this we can reuse this code for some other tasks also.

Example Fabfile for automating Tomcat setup


cat tomcat-user.xml

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
<?xml version='1.0' encoding='utf-8'?>
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema­instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
-->
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat" roles="manager-script,manager-gui"/>
</tomcat-users>
*************************************************************************

cat context.xml
<Context antiResourceLocking="false" privileged="true" >
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
</Context>

cat fabfile.py
# Author: Srini Vas

import boto

from fabric .api import *

env.hosts=['public ip of aws Instance']


env.user='ubuntu'
env.key_filename=['~/.ssh/tomcat-keypair.pem']

def update():
sudo("apt-get update -y")
sudo("sudo apt-get install default-jdk -y")

def tomcat8():
sudo("apt-get install tomcat8 -y")

def admin():
sudo ("apt-get install tomcat8-docs tomcat8-examples tomcat8-admin")

def user_tom():
put("tomcat-users.xml" , "/var/lib/tomcat8/conf", use_sudo = True)

def user_manage():
put("context.xml", "/var/lib/tomcat8/webapps/ROOT/META-INF/", use_sudo = True)

def start():
sudo("systemctl start tomcat8")

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
def ufw_stop():
sudo("service ufw stop")

def restart():
sudo("systemctl restart tomcat8")

def fab_tom():
update()
tomcat8()
admin()
user_tom()
user_manage()
start()
ufw_stop()

Now Tomcat8 is working


<ip of tomcat>:8080

Example fabfile for setting up Jenkins on a Ec2 instance.


# Author: Karunakar G

from fabric.api import *


env.hosts = ['IP']
env.user = 'ubuntu'
env.key_filename = ['~/.ssh/jenkins_server_key.pem']

def add_key_rep():
sudo("wget -q -O - https://pkg.jenkins.io/debian/jenkins­ci.org.key | sudo apt-key add -")
sudo("sudo sh -c 'echo deb http://pkg.jenkins.io/debian­stable binary/ >
/etc/apt/sources.list.d/jenkins.list'")
sudo("sudo add-apt-repository ppa:openjdk-r/ppa -y")

def apt_get_update():
sudo("sudo apt-get update")

def jenkins_git_maven_install():
sudo("sudo apt-get install openjdk-7-jdk -y")
sudo("sudo apt-get install jenkins -y")
sudo("sudo apt-get install git -y")
sudo("sudo apt-get install maven -y")

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
def jenkins_start():
sudo("sudo systemctl start jenkins")

def jenkins_git_maven_status():
sudo("sudo service jenkins status")

def jenkins_restart():
sudo("sudo service jenkins restart")

def jenkins_firewall():
sudo("service ufw stop")
def jenkins_setup():
add_key_rep()
apt_get_update()
jenkins_git_maven_install()
jenkins_start()
jenkins_git_maven_status()
jenkins_firewall()

12. Boto for AWS


Boto is a python library which provides an interface to interact wth AWS services.
Installation
$ sudo pip install boto
Configuration
We need to setup AWS authentication, so boto get authenticated to AWS services.
In order to do that an IAM user has to be created with programmatic access.
Create a ~/.boto file with below syntax:
[Credentials]
aws_access_key_id = YOURACCESSKEY
aws_secret_access_key = YOURSECRETKEY
Creating a connection for S3
>>> import boto

>>> conn = boto.connect_s3()

Creating a bucket
>>> bucket = s3.create_bucket('boto-demo-%s' % int(time.time()))

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Accessing A Bucket
>>> mybucket = conn.get_bucket('mybucket') # Substitute in your bucket name

>>> mybucket.list()

...listing of keys in the bucket..


Deleting A Bucket
Removing a bucket can be done using the delete_bucket method. For example:
>>> conn.delete_bucket('mybucket') # Substitute in your bucket name

Ec2 Interface
Creating a Connection
The first step in accessing EC2 is to create a connection to the service. The recommended way of
doing this in boto is:
>>> import boto.ec2
>>> conn = boto.ec2.connect_to_region("us-west-2")

At this point the variable conn will point to an EC2Connection object.

Launching Instances
To launch an instance and have access to it, you need to first setup security group and key pair.

Now, let’s say that you already have a key pair, want a specific type of instance, and you have your
security group all setup. In this case we can use the keyword arguments to accomplish that:

>>> conn.run_instances(
'<ami-image-id>',
key_name='your-key-name-here',
instance_type='t2.micro',
security_groups=['your-security-group-here'])

Stopping Instances
If you want to stop/shut down an instance, get its instance id and supply that as keyword argument.

>>> conn.stop_instances(instance_ids=['instance-id-1','instance-id-2', ...])

Terminating Instances
Similar to terminate an instance pass istance id’s.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
>>> conn.terminate_instances(instance_ids=['instance-id-1','instance-id-2',
...])

Checking What Instances Are Running

Using Elastic block storage.


EBS Basics
EBS can be used by EC2 instances for permanent storage. Note that EBS volumes must be in the
same availability zone as the EC2 instance you wish to attach it to.
>>> vol = conn.create_volume(50, "us-west-2")
>>> vol
Volume:vol-0000000
>>> curr_vol = conn.get_all_volumes([vol.id])[0]
>>> curr_vol.status
u'available'
>>> curr_vol.zone
u'us-west-2'
We can now attach this volume to the EC2 instance we created earlier, making it available as a new
device:

>>> conn.attach_volume (vol.id, inst.id, "/dev/sdx")


u'attaching'

Working With Snapshots


Snapshots allow you to make point-in-time snapshots of an EBS volume for future recovery.
Snapshots allow you to create incremental backups, and can also be used to instantiate multiple new
volumes. Snapshots can also be used to move EBS volumes across availability zones or making
backups to S3.
Creating a snapshot is easy:
>>> snapshot = conn.create_snapshot(vol.id, 'My snapshot')
>>> snapshot
Snapshot:snap-00000000
Once you have a snapshot, you can create a new volume from it. Volumes are created lazily from
snapshots, which means you can start using such a volume straight away:

>>> new_vol = snapshot.create_volume('us-west-2')


>>> conn.attach_volume (new_vol.id, inst.id, "/dev/sdy")

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
u'attaching'
If you no longer need a snapshot, you can also easily delete it:

>>> conn.delete_snapshot(snapshot.id)
True

A sample Boto script to spin up instance and attach EIP to it.


#!/usr/bin/python

import boto

import boto.ec2, time

## Variables

region =

ami_id =

login_key_name =

inst_type =

sbnet_id =

inst_name =

# Establishes connection with aws ec2 service.

conn = boto.ec2.connect_to_region(region)

# Spins up the ec2 instance.

ins = conn.run_instances(ami_id, key_name=login_key_name,


instance_type=inst_type, subnet_id=sbnet_id)

# Gets the instance id.

inst_id = ins.instances[0]

inst = str(inst_id)

# Allocates Elastic IP.

eip = conn.allocate_address(domain='vpc')

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
eip = str(eip)

# Prints status information.

print "%s is spiniing, please wait." % inst

status = inst_id.update()

print "Instance state %s" % status

# Checks the status of the instance.

while status == 'pending':

time.sleep(10)

status = inst_id.update()

print "Instance state %s" % status

# If the instance status is running assigns elastic IP to it.

if status == 'running':

inst_id.add_tag("Name",inst_name)

conn.associate_address(inst[9:],eip[8:])

print "<Instance Name> is running"

print "Elastic IP -- %s" % eip

print "Instance ID -- %s" % inst

else:

print('Instance status: ' + status)

A Sample Dynamo DB table creation script.

DynamoDB is AWS NOSQL DB service.


Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable
performance with seamless scalability. DynamoDB lets you offload the administrative burdens of
operating and scaling a distributed database, so that you don't have to worry about hardware
provisioning, setup and configuration, replication, software patching, or cluster scaling.

import boto.dynamodb2

from boto.dynamodb2 import *

from boto.dynamodb2.fields import HashKey, RangeKey,KeysOnlyIndex,GlobalAllIndex

from boto.dynamodb2.table import Table

import sys

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
conn = boto.dynamodb2.connect_to_region('us-west-2',aws_access_key_id='',
aws_secret_access_key='')

env='Tesla'

def except_func(e):

e_dict=e.__dict__['message']

if 'Table already exists' in e_dict:

print e_dict+', skipping create table.'

return None

else:

print e_dict

print "exiting db create script"

sys.exit()

#sprint4

try:

UserDisLikeProducts = Table.create(env+'AltCurrent',
schema=[HashKey('UserId'),RangeKey('ProductUrl')], connection=conn);

except Exception as e:

print except_func(e)

print "Hey yo"

A Sample boto script to spin Beanstalk Windows .net platform


#!/usr/bin/python

import boto

import boto.beanstalk, time

import jenkins

import getpass

### Creating beanstalk instance

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
def bean_create(bean_name):

conn = boto.beanstalk.connect_to_region('us-west-2')

bean_info = conn.describe_applications(application_names=bean_name)

bean_list_info =
bean_info['DescribeApplicationsResponse']['DescribeApplicationsResult']['Applica
tions']

if not bean_list_info:

print "Creating beanstalk application %s" % bean_name

else:

print "%s stalk already exists, please delete the stalk and run the script
again." % bean_name

exit()

conn.create_application(bean_name, description=bean_name)

conn.create_application_version(bean_name, 'Sample_Application')

conn.create_environment(bean_name, bean_name,
version_label='Sample_Application', solution_stack_name='64bit Windows Server
2012 R2 running IIS 8.5', cname_prefix=bean_name, description=None,
option_settings=[('aws:autoscaling:launchconfiguration', 'Ec2KeyName',
'testdigitaltestApp'), ('aws:autoscaling:launchconfiguration',
'IamInstanceProfile', 'aws-elasticbeanstalk-ec2-
role'),('aws:autoscaling:updatepolicy:rollingupdate', 'RollingUpdateEnabled')],
options_to_remove=None, tier_name='WebServer', tier_type='Standard',
tier_version='1.0')

time.sleep(10)

dict = conn.describe_events(application_name=bean_name,
environment_name=bean_name)

time.sleep(3)

event1 =
dict['DescribeEventsResponse']['DescribeEventsResult']['Events'][0]['Message']

event2 =
dict['DescribeEventsResponse']['DescribeEventsResult']['Events'][0]['Message']

print event1

while 'Successfully' not in event1:

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
time.sleep(5)

dict = conn.describe_events(application_name=bean_name,
environment_name=bean_name)

if 'Error' in event1:

print event1

print "Encountered error, please wipe out %s application from AWS


beanstalk and start over again" % bean_name

exit()

if event1 != event2:

print event2

event1 =
dict['DescribeEventsResponse']['DescribeEventsResult']['Events'][0]['Message']

event2 =
dict['DescribeEventsResponse']['DescribeEventsResult']['Events'][0]['Message']

Summary:

• Python is an interpreted language, its highly extensible and comes with lot of goodies.

• Python is very easy to read and write compared to any other programming language.

• Numbers, Strings, List, Tuples & Dictionary are python data types. Python comes with lot
of methods that be applied on these datatypes to manipulate or present data in different
ways.

• Decision making(if/elif/else) and loops(for&while) in python has a very simple syntax and
block of code inside them are indented mostly two or three spaces.

• Python functions are used when we want to group some code together and have it accessible
from any other python code. We can use these functions also as modules and import them
into other python scripts. This gives us a very modular structure for our complex code.

• OS python library is used to run system commands from python scripts like cd, mkdir,
chmod, cp, mv etc. There are other libraries as well for system tasks like sub process and
commands.

• Fabric is a python library used to run system commands on local and remote system. Most
of the python code is abratracted from us, we just call fabric functions for automating linux
tasks on local and remote system.

• Boto is a python system library to call, operate and manage AWS services from python.

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.
Conclusion:
Being so versatile in nature, managing huge python code becomes difficult. When it comes to
sharing our code with our team mates or other teams it is a big problem. Everybody have their own
style of coding and most of us while doing automation dont follow any best practices which makes
code difficult to read and understand. That is the reason world is moving or moved towards
configuration management tools for doing automation. Diffrences will be discussed in Ansible
chapter.
Some important links.
For practicing python with exercises.
https://www.codecademy.com/learn/python

Posting errors and questions.


https://stackoverflow.com/

Fabric documentation.
https://docs.fabric.io/

Boto Documentation
https://boto3.readthedocs.io/en/latest/

Visualpath Training & Consulting.


Flat no: 205, Nilgiri Block,Aditya Enclave, Ameerpet, Hyderabad, Phone No: - +91-970 445 5959, 961 824 5689 E-
Mail ID : [email protected], Website : www.visualpath.in.

You might also like