Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Enterprise Automation with Python

You're reading from   Hands-On Enterprise Automation with Python Automate common administrative and security tasks with Python

Arrow left icon
Product type Paperback
Published in Jun 2018
Publisher Packt
ISBN-13 9781788998512
Length 398 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bassem Aly Bassem Aly
Author Profile Icon Bassem Aly
Bassem Aly
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Setting Up Our Python Environment 2. Common Libraries Used in Automation FREE CHAPTER 3. Setting Up the Network Lab Environment 4. Using Python to Manage Network Devices 5. Extracting Useful Data from Network Devices 6. Configuration Generator with Python and Jinja2 7. Parallel Execution of Python Script 8. Preparing a Lab Environment 9. Using the Subprocess Module 10. Running System Administration Tasks with Fabric 11. Generating System Reports and System Monitoring 12. Interacting with the Database 13. Ansible for System Administration 14. Creating and Managing VMware Virtual Machines 15. Interacting with the OpenStack API 16. Automating AWS with Boto3 17. Using the Scapy Framework 18. Building a Network Scanner Using Python 19. Other Books You May Enjoy

An introduction to Python

Python is a high-level programming language that provides a friendly syntax; it is easy to learn and use, for both beginner and expert programmers.

Python was originally developed by Guido van Rossum in 1991; it depends on a mix of C, C++, and other Unix shell tools. Python is known as a language for general purpose programming, and today it's used in many fields, such as software development, web development, network automation, system administration, and scientific fields. Thanks to its large number of modules available for download, covering many fields, Python can cut development time down to a minimum.

The Python syntax was designed to be readable; it has some similarities to the English language, while the code construction itself is beautiful. Python core developers provide 20 informational rules, called the Zen of Python, that influenced the design of the Python language; most of them involve building clean, organized, and readable code. The following are some of the rules:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

You can read more about the Zen of Python at https://www.python.org/dev/peps/pep-0020/.

Python versions

Python comes with two major versions: Python 2.x and Python 3.x. There are subtle differences between the two versions; the most obvious is the way their print functions treat multiple strings. Also, all new features will only be added to 3.x, while 2.x will receive security updates before full retirement. This won't be an easy migration, as many applications are built on Python 2.x.

Why are there two active versions?

I will quote the reason from the official Python website:

"Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.

"Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x)."

You can read more about this topic at https://wiki.python.org/moin/Python2orPython3.

Should you only learn Python 3?

It depends. Learning Python 3 will future-proof your code, and you will use up-to-date features from the developers. However, note that some third-party modules and frameworks lack support for Python 3 and will continue to do so for the near future, until they completely port their libraries to Python 3.

Also, note that some network vendors, such as Cisco, provide limited support for Python 3.x, as most of the required features are already covered in Python 2.x releases. For example, the following are the supported Python versions for Cisco devices; you will see that all devices support 2.x, not 3.x:

Does this mean I can't write code that runs on both Python 2 and Python 3?

No, you can, of course, write your code in Python 2.x and make it compatible with both versions, but you will need to import a few libraries first, such as the __future__ module, to make it backward compatible. This module contains some functions that tweak the Python 2.x behavior and make it exactly like Python 3.x. Take a look at the following examples to understand the differences between the two versions:

#python 2 only
print "Welcome to Enterprise Automation"

The following code is for Python 2 and 3:

# python 2 and 3
print("Welcome to Enterprise Automation")

Now, if you need to print multiple strings, the Python 2 syntax will be as follows:

# python 2, multiple strings
print "welcome", "to", "Enterprise", "Automation"

# python 3, multiple strings
print ("welcome", "to", "Enterprise", "Automation")

If you try to use parentheses to print multiple strings in Python 2, it will interpret it as a tuple, which is wrong. For that reason, we will import the __future__ module at the beginning of our code, to prevent that behavior and instruct Python to print multiple strings.

The output will be as follows:

Python installation

Whether you choose to go with a popular Python version (2.x) or build future-proof code with Python 3.x, you will need to download the Python binaries from the official website and install them in your operating system. Python provides support for different platforms (Windows, Mac, Linux, Raspberry PI, and so on):

  1. Go to https://www.python.org/downloads/ and choose the latest version of either 2.x or 3.x:
  1. Choose your platform from the Download page, and either the x86 or x64 version:
  1. Install the package as usual. It's important to select the Add python to the path option during the installation, in order to access Python from the command line (in the case of Windows). Otherwise, Windows won't recognize the Python commands and will throw an error:
  1. Verify that the installation is complete by opening the command line or terminal in your operating system and typing python. This should access the Python console and provide a verification that Python has successfully installed on your system:
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime